选择时突出显示 R 闪亮按钮的边框或颜色

Highlight border or color of R shiny button when selected

有人知道如何在选中闪亮按钮时突出显示边框或颜色吗?

请在下方查找可重现的示例

library(shiny)

ui <- fluidPage(
  actionButton(inputId = "button1", label = "Select red"),
  actionButton(inputId = "button2", label = "Select blue"),
  plotOutput("distPlot")
)


server <- function(input, output) {
    r <- reactiveValues(my_color = "green")

    output$distPlot <- renderPlot({
      x <- faithful[, 2]
      bins <- seq(min(x), max(x))
      hist(x, breaks = bins, col = r$my_color)
    })

   observeEvent(input$button1, {
      r$my_color <- "red"
    })

  observeEvent(input$button2, {
    r$my_color <- "blue"
   })
}

shinyApp(ui = ui, server = server)

下面是上面的代码得到的结果:

下面是当按钮 "Select red" 被选中时我想要得到的(请注意,它应该在选中时突出显示另一个按钮):

如果这不可能,是否有一种方法可以在选中时更改按钮颜色?

提前致谢

您可以使用 shinyjs 包在按钮上 add/remove CSS classes。这里我使用 Bootstrap 中的 btn-primary class 将按钮设为蓝色,但您也可以添加自己的样式。

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  actionButton(inputId = "button1", label = "Select red"),
  actionButton(inputId = "button2", label = "Select blue"),
  plotOutput("distPlot")
)


server <- function(input, output) {
  r <- reactiveValues(my_color = "green")

  output$distPlot <- renderPlot({
    x <- faithful[, 2]
    bins <- seq(min(x), max(x))
    hist(x, breaks = bins, col = r$my_color)
  })

  observeEvent(input$button1, {
    removeClass("button2", "btn-primary")
    addClass("button1", "btn-primary")
    r$my_color <- "red"
  })

  observeEvent(input$button2, {
    removeClass("button1", "btn-primary")
    addClass("button2", "btn-primary")
    r$my_color <- "blue"
  })
}

shinyApp(ui = ui, server = server)