【发布时间】:2016-08-26 04:43:44
【问题描述】:
我正在尝试模块化一个复杂的 Shiny 应用程序,我有一个 conditionalPanel 应该只在给定特定输入状态时出现。
在我将所有内容模块化之前,输入和条件面板都在ui.R 中,我可以使用以下方式引用输入:
conditionalPanel("input.select == 'Option one'", p('Option one is selected'))
现在我已经模块化了东西,访问输入更加复杂。我认为以下是做到这一点的方法,但它并不完全奏效。 (这里我把东西组合成一个独立的脚本):
library(shiny)
## Module code for 'selectorUI' and 'selector'
selectorUI <- function(id) {
ns <- NS(id)
selectizeInput(inputId = ns('select'),
label = 'Make a choice:',
choices = c('Option one', 'Option two'))
}
selector <- function(input, output, session) {
reactive(input$select)
}
## Main app
ui <- shinyUI(fluidPage(
selectorUI('id1'),
conditionalPanel(condition = "output.selected == 'Option one'", p('Option one is selected.'))
))
server <- shinyServer(function(input, output, session) {
output$selected <- callModule(selector, 'id1')
})
shinyApp(ui = ui, server = server)
我认为这应该可行,但它不起作用 - 只有当我在主 ui 部分再次引用 output$selected 时它才有效:
ui <- shinyUI(fluidPage(
selectorUI('id1'),
textOutput('selected'), ## Adding just this one line makes the next line work
conditionalPanel(condition = "output.selected == 'Option one'", p('Option one is selected.'))
))
不幸的是,这当然会产生渲染textOutput('selected') 的结果的不良影响。我只能猜测它起作用的原因是因为它以某种方式触发了响应式,而 JavaScript 引用本身不会。
知道我应该如何让这个条件面板正常工作吗?
谢谢你..
编辑:原来实际上不是一个错误:https://github.com/rstudio/shiny/issues/1318。请参阅下面我自己的答案。
但还要注意,我实际上比我原来的conditionalPanel 方法更喜欢接受答案中给出的renderUI 解决方案。
【问题讨论】: