我无法理解conditionalPanel()
的一些奇怪行为。根据用户输入,应显示部分不同的消息。在任何情况下都应该显示一个元素。见例:
## Only run this example in interactive R sessions
if (interactive()) {
ui <- fluidPage(
sidebarPanel(
selectInput("option", "Select option",
c("A", "B", "C"),
selected = "B"
)
),
mainPanel(
uiOutput("test0"),
uiOutput("test1")
)
)
server <- function(input, output) {
output$msg <- renderText("Some text")
output$test0 <- renderUI({
conditionalPanel(
condition = "input.option == 'B' || input.option == 'C'",
verbatimTextOutput("msg"),
wellPanel(h4("Are you sure you don't want to select A?")))
})
output$test1 <- renderUI({
conditionalPanel(
condition = "input.option == 'A'",
verbatimTextOutput("msg"),
wellPanel(h4("Good choice!")))
})
}
shinyApp(ui, server)
}
这个迷你示例中的明显解决方案是将verbatimTextOutput()
移到conditionalPanel()
之外,但我想知道是否有其他方法可以解决此问题。
答案 0 :(得分:1)
为什么不将它们组合成相同的输出
dropdown.item
无需output$test0 <- renderUI({
list(
verbatimTextOutput("msg"),
conditionalPanel(
condition = "input.option == 'B' || input.option == 'C'",
wellPanel(h4("Are you sure you don't want to select A?"))),
conditionalPanel(
condition = "input.option == 'A'",
wellPanel(h4("Good choice!")))
)
})
。然后只会有一个output$test1
答案 1 :(得分:1)
您不能让两个输出共享相同的ID。但是,您可以为两个不同的输出定义相同的render
函数,在您的情况下将是什么解决方案。只需为您的verbatimTextOutputs
和一个render
功能分配不同的ID即可。
ui <- fluidPage(
sidebarPanel(
selectInput("option", "Select option",
c("A", "B", "C"),
selected = "B"
)
),
mainPanel(
uiOutput("test0"),
uiOutput("test1")
)
)
server <- function(input, output) {
output$msg1 <- output$msg2 <- renderText("Some text")
output$test0 <- renderUI({
conditionalPanel(
condition = "input.option == 'B' || input.option == 'C'",
verbatimTextOutput("msg1"),
wellPanel(h4("Are you sure you don't want to select A?")))
})
output$test1 <- renderUI({
conditionalPanel(
condition = "input.option == 'A'",
verbatimTextOutput("msg2"),
wellPanel(h4("Good choice!")))
})
}
shinyApp(ui, server)