简体   繁体   English

按钮对 Shiny 应用程序中的 slideBar 输入的更改没有反应

[英]Button is not reactive to changes to slideBar input in Shiny app

I am trying to let shiny do a calculation with the slide bar value after the user clicks on the action button.我试图让 shiny 在用户单击操作按钮后使用滑动条值进行计算。 After the action button has been clicked, the app should then show a message.单击操作按钮后,应用程序应显示一条消息。 At the moment I've got the following code running, but after you click the Action Button for the first time, it will automatically re-execute when the reactive slide bars change.目前我已经运行了以下代码,但是在您第一次单击 Action Button 后,当反应性滑动条发生变化时,它将自动重新执行。 Does someone have a suggestion to make it so that the action button must be clicked every time the slide bar input changes?是否有人建议在每次滑动条输入更改时都必须单击操作按钮?

shinyApp(
  ui = fluidPage(
    div(
      id = "form",
      sliderInput("popDensity", "What is the population density?", value=0, min=0, max=1500),
      
      actionButton("button", "Evaluate", style="color: #fff; background-color: #337ab7; border-color: #2e6da4")
      
    ),
    div(
      id = "sidebar",
      sidebarPanel(
                   uiOutput("calculation")   ) )
  ),
  
  server = function(input, output) {
    likelihood<-reactive({(1/500)*input$popDensity})
    
    observeEvent(input$button, {
      
      output$calculation<-renderUI({
        if (likelihood()>1) {
          a <- paste("<span style=color:red> be careful! </span>")
        }
          else if (likelihood()>.65){
            a <- paste("<span style=color:orange> be careful! </span>")
          }
        else if (likelihood()>.35){
          a <- paste("<span style=color:yellow> be careful! </span>")
        }
        else if (likelihood()>.10){
          a <- paste("<span style=color:blue> be careful! </span>")
        }
               else {
          a <- paste("<span style=color:green> be careful! </span>")
        }
        HTML(a)
        
      })
    })  
  }
)

If you want the UI to update only when the button is clicked, then likelihood doesn't need to be reactive.如果您希望 UI 仅在单击按钮时更新,那么likelihood不需要是响应式的。 So you can write your server function as所以你可以把你的服务器 function 写成

  server = function(input, output) {
    observeEvent(input$button, {
      likelihood<-(1/500)*input$popDensity
      output$calculation<-renderUI({
        if (likelihood>1) {
          a <- paste("<span style=color:red> be careful! </span>")
        }
        else if (likelihood>.65){
          a <- paste("<span style=color:orange> be careful! </span>")
        }
        else if (likelihood>.35){
          a <- paste("<span style=color:yellow> be careful! </span>")
        }
        else if (likelihood>.10){
          a <- paste("<span style=color:blue> be careful! </span>")
        }
        else {
          a <- paste("<span style=color:green> be careful! </span>")
        }
        HTML(a)
      })
    })  
  }

and get the effect you want.并得到你想要的效果。

Alternatively, if likelihood needs to remain reactive for reasons you haven't explained, then you need to isolate it from its dependency on the slider and introduce a dependency on the button into the renderUI call.或者,如果likelihood由于您未解释的原因需要保持响应,那么您需要将其与其对 slider 的依赖隔离开,并将对按钮的依赖引入到renderUI调用中。 Both are easy to do.两者都很容易做到。

You can isolate a widget from a dependency on an input using isolate :您可以使用隔离将小部件与输入的依赖isolate

likelihood<-reactive({(1/500)*isolate(input$popDensity)})

Introducing the dependency on the button is equally easy: just add a reference to input$button anywhere in the renderUI call.引入对按钮的依赖同样简单:只需在renderUI调用中的任意位置添加对input$button的引用。 This is often done at the top of the call:这通常在调用顶部完成:

output$calculation<-renderUI({
  input$button
  <your existing code here>
})

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

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