我想模块化一个复杂的Shiny应用程序,我有一个conditionalPanel
应该只显示给定的输入状态。R带条件面板和反应的闪亮模块
之前我做了一切模块化,输入和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)
我认为这应该工作,但它没有 - 如果我再拍参考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。在下面看到我自己的答案。
但也请注意,我实际上喜欢接受答案中给出的renderUI
解决方案比我的原始conditionalPanel
方法更好。
这确实有效,但是这不违反模块化的概念吗?您必须知道模块的代码在内部调用输入'select'才能构建'id1-select'。 –
是的,你是对的 - 我更新了我的文章。 –
我认为这确实是一个错误。我通过https://github.com/rstudio/shiny/issues/1318提供了一个更简单的例子。我更喜欢'renderUI'解决方案,因为'conditionalPanel'依赖于'display:none'风格来隐藏内容,而'renderUI'方法只是给出一个空的div。谢谢你写了。 –