如何在不失效的情况下更改按钮的属性

How to change an attribute of a button without invalidating

我想在 R/Shiny 中更改按钮的颜色而不使其失效。 具体来说,如果执行参数发生变化,我希望按钮为红色,但仅在按下按钮时才执行。

我把reactive里的参数隔离了,只对button的变化做出反应:

# react on button pressed (unfortunately also if color is changed
input$Button
# don't react on parameters changes
paramet <- isolate(input$parameter)

在观察者中,我检查参数并更改颜色:

if (!modified) {
      # cat(file = stderr(), "\n\ntsne not modified\n\n\n")
      actionButton(name, "apply changes", width = '80%', 
                   style = "color: #fff; background-color: #00b300; border-color: #2e6da4")
    } else {
      # cat(file = stderr(), "\n\ntsne modified\n\n\n")
      actionButton(name, "apply changes", width = '80%', 
                   style = "color: #fff; background-color: #cc0000; border-color: #2e6da4")
    }

但这会以某种方式触发按钮。

我唯一能想到的就是创建一个只有在按下按钮时才会改变的新反应。 IE。在我的观察者中,如果按钮值增加,我会更改反应值,这意味着,我将不得不将旧值存储在项目特定的变量中。我希望有一个更简单的方法来完成这个。

感谢您的帮助。

将 shinyjs 与 addClass(ButtonName, cssElement)removeClass(ButtonName, cssElement)、[cssElement = 'red' | 结合使用 | 'green'] 和 UI:

shinydashboard::dashboardBody(
      shinyjs::useShinyjs(debug = TRUE),
      inlineCSS(list(.red = "background: red")),
      inlineCSS(list(.green = "background: green")),
...

事实上,我使用的是 actionButton 而不是 updateActionButton,它正在重新初始化 actionButton,导致一些不需要的副作用,并且 updateActionButton 不允许更改颜色。