有人知道如何在选择 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)
以下是上面代码的结果:
这是当选择“选择红色”按钮时我想要得到的结果(请注意,选择时它应该突出显示另一个):
如果这是不可能的,是否存在一种方法可以在选择时更改按钮颜色?
提前致谢
最佳答案
您可以使用 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)
关于r - 选中时突出显示 R Shiny 按钮的边框或颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50965843/