如何在 R Shiny 中的两个单独选项卡中输入 ID 并反映不同的结果

ali*_*boy 2 r shiny

这可能是一个普遍的问题,我会尽力明确地描述它。在 R Shiny 和ui.R文件中,我使用radioButtons两种方法之一来选择:

  radioButtons("Methods", strong("Choose a Method:"),
                 choices = list("method_1" = "m1",
                                "method_2" = "m2"),
                 selected="method_1"),

  selectInput("method_2_ID", strong("Choose an ID (method_2"),
                topIDs)

  mainPanel(
    tabsetPanel(
      tabPanel(title = "method_1_tab1", 
               plotOutput("plots"), 
      tabPanel(title = "method_2_output1", 
               tableOutput("m2_output1")),
      tabPanel(title = "method_2_output2", 
               verbatimTextOutput("m2_output2")))
    ))
Run Code Online (Sandbox Code Playgroud)

你可以看到对于method_2,我打算使用两个不同的标签来显示不同的结果,即m2_output1m2_output2。在我的server.R文件中,我使用:

if (input$Methods == "method_2") {

  # m2_output1
  updateTabsetPanel(session, "method_2_output1", selected="panel2")

  # drop-down menu
  SelectedID = reactive(function(){
    input$method_2_ID
  })

  # m2_output1
  output$m2_output1 = renderTable({
    m2[m2$ID == input$method_2_ID, ]
  })

  # m2_output2
  updateTabsetPanel(session, "method_2_output2", selected="panel3")

  [...]
  output$m2_output2 = renderPrint({
     [...]
    }
  })
Run Code Online (Sandbox Code Playgroud)

但是,method_2_output1当我从下拉菜单中单击 ID 时,它仅适用于选项卡,并且当我单击method_2_ouptut2选项卡时,没有显示任何内容(verbatimTextOutput("m2_output2)"我认为应该显示)。我的ui.Rserver.R文件有什么问题吗?

Ram*_*han 6

需要进行很多更改才能使选项卡执行您想要的操作。

一些较大的变化是:

  1. 您必须使用参数创建tabsetPanel和,以便可以引用它们。tabPanelsid

  2. 为了updateTabsetPanel工作,您必须使用以下命令将这些命令包装在反应式观察器中:

    observe ( {
     #updateTabsetPanel here
    })
    
    Run Code Online (Sandbox Code Playgroud)
  3. 使用sessions参数调用 ShinyServer

我做了一些其他的改变。下面是一个完整的工作框架。当您选择方法 1 时,它选择 Tab1。如果选择方法 2,则根据选择的方法 ID 在第二个和第三个选项卡之间切换。 在此处输入图片说明

用户界面

library("shiny")

shinyUI(pageWithSidebar(    
  headerPanel("Tab Switch Demo"),
  sidebarPanel(
         h4('Switch Tabs Based on Methods'),
             radioButtons(inputId="method", label="Choose a Method",
                          choices = list("method_1",
                                         "method_2")),
         conditionalPanel("input.method== 'method_2' ",                          
                          selectInput("method_2_ID", strong("Choose an ID for method 2"),
                                      choices = list("method_2_1",
                                                     "method_2_2"))
                          )       

    ),
  mainPanel(
          tabsetPanel(id ="methodtabs",
            tabPanel(title = "First Method Plot", value="panel1",
                     plotOutput("method_1_tab1")),
            tabPanel(title = "method_2_output1", value="panel2",
                     tableOutput("m2_output1")),
            tabPanel(title = "method_2_output2", value="panel3",
                     verbatimTextOutput("m2_output2"))
            )
      )  
))
Run Code Online (Sandbox Code Playgroud)

服务器端

library('shiny')    
shinyServer(function(input, output, session) {
  output$method_1_tab1 = renderPlot({
    plot(cars)
  })
  output$m2_output1 = renderText({
    "First Tab for Method 2, ID=1"
  })
  output$m2_output2 = renderText({
    "Second Tab for Method 2, ID=2"
  })

  observe({
    if (input$method == "method_1") {    
      updateTabsetPanel(session, inputId="methodtabs", selected="panel1")
    }      
    else if (input$method_2_ID == "method_2_1") {    
      updateTabsetPanel(session, inputId="methodtabs", selected="panel2")
    }      
    else if (input$method_2_ID == "method_2_2") {    
      updateTabsetPanel(session, inputId="methodtabs", selected="panel3")
    }      
  }) 
Run Code Online (Sandbox Code Playgroud)

使用上面的代码作为起点,并开始进行更改。

希望能帮助你开始。