简体   繁体   English

观察事件以隐藏闪亮的动作按钮

[英]Observe Event to Hide Action Button in Shiny

In my Shiny application, I am trying to include logic to show or hide an action button depending on whether another user input in ui.R is defined. 在我的Shiny应用程序中,我试图包含显示或隐藏操作按钮的逻辑,具体取决于是否定义了ui.R中的另一个用户输入。 I cannot use the uiOutput/renderUI functionality to do this because of some other complexities in the application. 由于应用程序中的一些其他复杂性,我无法使用uiOutput / renderUI功能来执行此操作。

My approach is to create an observer for an input and then accordingly show or hide the action button using CSS tags. 我的方法是为输入创建一个观察者,然后使用CSS标记显示或隐藏操作按钮。 I don't know CSS and hence the struggle. 我不知道CSS因此而斗争。

This is the ui form in my application - 这是我申请中的ui表格 -

在此输入图像描述

Now, I have a reactive function locationSpecified which returns whether the input location is empty or not and based on this I have to show or hide the "RUN" button. 现在,我有一个反应函数locationSpecified ,它返回输入位置是否为空,基于此我必须显示或隐藏“运行”按钮。

Here is how I am adding the "RUN" button in ui.r ... 这是我在ui.r中添加“RUN”按钮的方法...

fluidRow(column(6, align="center", offset = 3, actionButton("action", "RUN")))

This is what I am trying in server.R (but obviously not working) ... 这是我在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}"))
  })

I am hoping that fix to this is not that complicated and I would appreciate if you can tell me how to get this to work. 我希望解决这个问题并不复杂,如果你能告诉我如何让它发挥作用,我将不胜感激。

Thanks in advance, 提前致谢,

Ashish 阿希什

Got it to finally work using shinyjs. 得到它最终使用shinyjs工作。

#ui
useShinyjs(),
fluidRow(column(6, align="center", offset = 3, actionButton("action", "RUN"))))

#server
observe({
  shinyjs::hide("action")

  if(locationSpecified())
     shinyjs::show("action")
})

Thanks carl for pointing me to shinyjs. 感谢卡尔指着我shinyjs。

You could use a conditionalPanel like so: 您可以像这样使用conditionalPanel

#UI
conditionalPanel(condition='input.location!=null && input.location!=""',
fluidRow(column(6, align="center", offset = 3, actionButton("action", "RUN"))))

Or you could use shinyjs 's toggle : 或者你可以使用shinyjstoggle

#UI
useShinyjs() # include useShinyjs() somewhere in UI
#server
observe({ toggle(id="action", condition=!is.null(input$location))})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM