如何在R中的两个单独选项卡中输入ID并反映不同的结果
这可能是一个一般性的问题,我将尽最大努力明确地描述它。在R和如何在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"),
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希望这能帮助您继续。您的
输出$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")
}
})