2

This maybe a general question and I will try my best to describe it explicitly. In R Shiny and the ui.R file, I use radioButtons to select one of the two methods:

  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")))
    ))

You can see for method_2, I plan to use two different tabs to show different results, i.e. m2_output1 and m2_output2. In my server.R file, I use:

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({
     [...]
    }
  })

However, it works only for the method_2_output1 tab when I click the ID from the drop-down menu, and when I click the method_2_ouptut2 tab, there is nothing displayed (should display verbatimTextOutput("m2_output2)", I think). Is there anything wrong with my ui.R or server.R files?

4

1 回答 1

6

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

一些较大的变化是:

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

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

    observe ( {
     #updateTabsetPanel here
    })
    
  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"))
            )
      )  
))

服务器.R

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")
    }      
  }) 

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

希望这可以帮助您继续前进。

于 2013-07-30T21:15:42.083 回答