Joh*_*ohn 2 r highlighting button shiny shinydashboard
有人知道如何在选中闪亮按钮时突出显示边框或颜色吗?
请在下面找到可复制的示例
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)
Run Code Online (Sandbox Code Playgroud)
这是上面代码的结果:
这是我想在选择“选择红色”按钮时得到的结果(请注意,选择时它应该突出显示另一个):
如果这是不可能的,是否存在在选择时更改按钮颜色的方法?
提前致谢
小智 5
您可以使用Shinyjs包在按钮上添加/删除 CSS 类。在这里,我使用btn-primaryBootstrap 中的类将按钮设为蓝色,但您也可以添加自己的样式。
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)
Run Code Online (Sandbox Code Playgroud)