简体   繁体   English

单击按钮时更改颜色动作按钮 Shiny R

[英]Change color actionButton when button clicked Shiny R

I have an actionButton defined in the UI as uiOutput("DartSearchAdv").我在 UI 中将 actionButton 定义为 uiOutput("DartSearchAdv")。 I would like the button to change color when input$target_min_ab is changed.当 input$target_min_ab 改变时,我希望按钮改变颜色。 And when input$DartSearchAdv is pressed, I want the color to go back to default.当按下 input$DartSearchAdv 时,我希望 go 的颜色恢复为默认值。

I've tried implementing the suggestion here , and the color change will work as desired, but certain outputs generated from the button press are hidden ("mainPanelDST").我已尝试在此处实施建议,颜色更改将按需要进行,但按钮按下生成的某些输出被隐藏(“mainPanelDST”)。 I can try getting them to appear if I do show("mainPanelDST"), but then the color change doesn't take place, or the button will disappear until input$target_min_ab is interacted with again.如果我确实显示(“mainPanelDST”),我可以尝试让它们出现,但是颜色变化不会发生,或者按钮将消失,直到再次与 input$target_min_ab 交互。

Below is the relevant code from the server.以下是来自服务器的相关代码。

global <- reactiveValues(clicked = FALSE)

# Desired style for when button is clicked
defaultColor = 'padding:10px; font-size:120%;
         color: white; background-color: #3E668E;
         border-color: #2C3E50'

# Desired style for when a setting is changed
updateColor = 'padding:10px; font-size:120%;
       color: white; background-color: #428BCA;
       border-color: #95A5A6'

# render the button
output$DartSearchAdv <- renderUI({
      if (global$clicked){
        actionButton("DartSearchAdv", "Update Search",
                     style = defaultColor)
      } else {
        actionButton("DartSearchAdv", "Update Search",
                     style = updateColor)
      }
      
    })

And here are the inputs that should change the color这是应该改变颜色的输入

# input that changes color to updateColor
observeEvent(input$target_min_ab,{
  rv$target_min_ab = input$target_min_ab/100; 
  global$clicked = FALSE; 
})

# input that changes color to default
trigger_button2 <- eventReactive(input$DartSearchAdv, {
# Do stuff in here
global$clicked = TRUE

}

Perhaps this:也许是这样:

library(shiny)

# Desired style for when button is clicked
defaultColor <- "padding:10px; font-size:120%;
         color: white; background-color: #3E668E;
         border-color: #2C3E50"

# Desired style for when a setting is changed
updateColor <- "padding:10px; font-size:120%;
       color: white; background-color: #428BCA;
       border-color: #95A5A6"


ui <- fluidPage(
  uiOutput("DartSearchAdv"), # will be an actionButton
  numericInput("target_min_ab", "Target Min Ab", 1),
  actionButton("DartSearchAdv", "DartSearchAdv")
)


server <- function(input, output, session) {
  global <- reactiveValues(clicked = FALSE)
  rv <- reactiveValues(target_min_ab = NULL)


  # render the button
  output$DartSearchAdv <- renderUI({
    if (global$clicked) {
      actionButton("DartSearchAdv", "Update Search",
        style = defaultColor
      )
    } else {
      actionButton("DartSearchAdv", "Update Search",
        style = updateColor
      )
    }
  })


  observeEvent(input$target_min_ab,
    {
      rv$target_min_ab <- input$target_min_ab / 100
      global$clicked <- TRUE
    },
    ignoreInit = TRUE
  )

  observeEvent(input$DartSearchAdv, {
    global$clicked <- FALSE
  })
}



shinyApp(ui, server)

在此处输入图像描述

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

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