【发布时间】:2017-07-12 23:22:01
【问题描述】:
我正在构建一个 shiny 网页,其表单由两个 selectInput 组成:第一个 - 静态 - 在 ui 部分,第二个 - 动态 - 在 server 部分。实际问题的简化如下所示。
require(shiny)
ui <- fluidPage(
# The static input
selectInput(inputId = 'static',
label = 'Make a choice',
choices = c('A', 'B', 'C'),
selectize = FALSE),
# The dynamic input
uiOutput(outputId = 'dynamicInput'),
# The output
hr(),
strong('This is a check for the output:'),
textOutput(outputId = 'check')
)
server <- function(input, output, session) {
# The dynamic input definition
output$dynamicInput <- renderUI({
# This input exists if the `static`
# one is equal to `A` only
if (input$static == 'A') {
selectInput(inputId = 'dynamic',
label = 'Choose a subset for `A`',
choices = c('A1', 'A2', 'A3'),
selectize = FALSE)
} else {
return(NULL)
}
})
# The example output
output$check <- renderText({
paste(input$static, input$dynamic)
})
}
shinyApp(ui, server)
在实际情况下,我必须根据静态输入以及最终的动态输入(如果存在)在数据库上启动查询。
在测试应用程序时,我在第一轮正确获得了动态 selectInput,并且我能够选择 - 比如说 - A2 选项。然后,如果我在静态selectInput中选择B,动态输入没有响应,仍然显示之前的选择而不是显示NULL。
如果静态不等于A,如何强制重置动态selectInput?我是否必须使用诸如Dean Attali 的shinyjs 之类的技巧手动隐藏它?
【问题讨论】:
-
您需要隐藏 UI(在您给出的示例中已经发生)还是需要“重置”
input$dynamic的值?shinyjs只会帮助你处理前者,但这已经发生了,至少在你给出的简化示例中 -
两者,但考虑到动态输入在不请求时消失,第一个已经可以了。我还希望动态
selectInput在静态不等于A时返回NULL。