在我的Shiny应用程序中,我试图包含显示或隐藏操作按钮的逻辑,具体取决于是否定义了ui.R中的另一个用户输入.由于应用程序中的一些其他复杂性,我无法使用uiOutput/renderUI功能来执行此操作.
我的方法是为输入创建一个观察者,然后使用CSS标记显示或隐藏操作按钮.我不知道CSS因此而斗争.
这是我申请中的ui表格 -
现在,我有一个反应函数locationSpecified
,它返回输入位置是否为空,基于此我必须显示或隐藏"运行"按钮.
这是我在ui.r中添加"RUN"按钮的方法...
fluidRow(column(6, align="center", offset = 3, actionButton("action", "RUN")))
Run Code Online (Sandbox Code Playgroud)
这是我在server.R尝试(但显然不工作)...
observe({
if(locationSpecified() == 1)
tags$head(tags$style(type="button/css", ".btn {display: inline-block}"))
if(locationSpecified() == 0)
tags$head(tags$style(type="button/css", ".btn {display: none}"))
})
Run Code Online (Sandbox Code Playgroud)
我希望解决这个问题并不复杂,如果你能告诉我如何让它发挥作用,我将不胜感激.
提前致谢,
阿希什
终于可以使用Shinyjs工作了。
#ui
useShinyjs(),
fluidRow(column(6, align="center", offset = 3, actionButton("action", "RUN"))))
#server
observe({
shinyjs::hide("action")
if(locationSpecified())
shinyjs::show("action")
})
Run Code Online (Sandbox Code Playgroud)
感谢卡尔为我指出了Shinyjs。
你可以这样使用conditionalPanel
:
#UI
conditionalPanel(condition='input.location!=null && input.location!=""',
fluidRow(column(6, align="center", offset = 3, actionButton("action", "RUN"))))
Run Code Online (Sandbox Code Playgroud)
或者你可以用shinyjs
's toggle
:
#UI
useShinyjs() # include useShinyjs() somewhere in UI
#server
observe({ toggle(id="action", condition=!is.null(input$location))})
Run Code Online (Sandbox Code Playgroud)