Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中的两个单独选项卡中输入ID并反映不同的结果_R_Shiny - Fatal编程技术网

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

如何在R中的两个单独选项卡中输入ID并反映不同的结果,r,shiny,R,Shiny,这可能是一个一般性的问题,我将尽最大努力明确地描述它。在R和ui.R文件中,我使用单选按钮选择两种方法之一: radioButtons("Methods", strong("Choose a Method:"), choices = list("method_1" = "m1", "method_2" = "m2"), selected="method_1"),

这可能是一个一般性的问题,我将尽最大努力明确地描述它。在R和
ui.R
文件中,我使用
单选按钮
选择两种方法之一:

  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")))
    ))
您可以看到,对于方法2,我计划使用两个不同的选项卡来显示不同的结果,即
m2\u输出1
m2\u输出2
。在我的
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({
     [...]
    }
  })

但是,当我从下拉菜单中单击ID时,它仅适用于
方法输出1
选项卡,当我单击
方法输出2
选项卡时,没有显示任何内容(我认为应该显示
逐字输出(“m2\u输出2”)
)。我的
ui.R
server.R
文件有什么问题吗?

需要做很多更改才能使选项卡实现您想要的功能

一些更大的变化是:

  • 必须使用
    id
    参数创建
    tabsetPanel
    tabPanels
    ,以便可以引用它们

  • 要使
    updateAbsetPanel
    正常工作,必须使用以下命令将这些命令包装到反应式观察者中:

    observe ( {
     #updateTabsetPanel here
    })
    
  • 使用
    sessions
    参数调用ShinyServer

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

    用户界面 服务器.R 使用上面的代码作为起点,并开始进行更改


    希望这能帮助您继续。

    您的
    输出$m2\u output2
    是否依赖于
    输入$method\u ID
    ?您的
    输出$m2\u output2
    中有什么内容?@sgibb:是的,我将在计算中使用
    输入$method\u ID
    (正如我在
    \m2\u output2
    下的
    […]
    部分中省略的那样。
    m2\u output2
    的输出只是一些打印结果。非常感谢您提供了详细的示例!它工作得很好;-)
    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"))
                )
          )  
    ))
    
    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")
        }      
      })