【问题标题】:Change color actionButton when button clicked Shiny R单击按钮时更改颜色动作按钮 Shiny R
【发布时间】:2022-01-15 12:15:47
【问题描述】:

我有一个在 UI 中定义为 uiOutput("DartSearchAdv") 的 actionButton。当 input$target_min_ab 改变时,我希望按钮改变颜色。当按下 input$DartSearchAdv 时,我希望颜色恢复默认值。

我已尝试实施建议 here,颜色更改将按需要进行,但按钮按下生成的某些输出被隐藏(“mainPanelDST”)。如果我确实显示(“mainPanelDST”),我可以尝试让它们出现,但是颜色不会发生变化,或者按钮将消失,直到再次与 input$target_min_ab 交互。

以下是来自服务器的相关代码。

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)
      }
      
    })

这里是应该改变颜色的输入

# 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

}

【问题讨论】:

    标签: r shiny reactive-programming


    【解决方案1】:

    也许是这样的:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-13
      • 2021-07-07
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      • 2012-12-06
      • 2014-08-26
      • 1970-01-01
      相关资源
      最近更新 更多