r - 选中时突出显示 R Shiny 按钮的边框或颜色

标签 r button shiny shinydashboard highlighting

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

请在下面找到一个可重现的示例

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)

以下是上面代码的结果:

enter image description here

这是当选择“选择红色”按钮时我想要得到的结果(请注意,选择时它应该突出显示另一个):

enter image description here

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

提前致谢

最佳答案

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

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)

Result

关于r - 选中时突出显示 R Shiny 按钮的边框或颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50965843/

相关文章:

r - 当trimws不起作用时如何修剪空格?

Java:为什么我的 JButton 被绘制在背景图像后面?

html - `min-width` 不适用于表单 `input[type="button"]` 元素吗?

r - 一旦用户在 R Shiny 中选择了多个选项,就禁用选择输入

r - Shiny.io 内存不足

r - 统计密度二维图的希腊字母

r - ggplot2 中的 cex 等价物

r - R中的向量化核函数

html - 如何设置按钮背景,使其占用父div的高度和宽度?什么CSS属性会影响哪个维度?

r - 在不添加和删除的情况下更新传单提供者磁贴选项