I would like to display several value boxes in a Shiny application. One of the valueboxes should change colors based on the selected value that will be displayed. In the example below, the color will change if input the value is above a certain threshold (4 in this case).
My questions are:
- How can I ensure that the valueboxes that are run on the server side will be the same size as the ones that are created only in the UI?
- Do I actually need to use shiny::uiOutput to achieve the dynamic change of color? Or is there a better way to update bslib::value_box without re-rendering the entire box?
My attempt below:
library(shiny)
library(bslib)
ui <- page_fixed(
shiny::selectInput(
inputId = "selected.events",
label = "Select events",
choices = c(1:10)
),
bslib::layout_columns(
row_heights = 1, # fixing row_heights seems to only control
# the value_box on the UI side, not the one
# on the server side
bslib::value_box(
title = "Box with static info",
value = ".."
),
shiny::uiOutput("events")
)
)
server <- function(input, output) {
output$events <- renderUI({
bslib::value_box(
title = "Events",
value = input$selected.events,
theme_color = if(as.numeric(input$selected.events) > 4) "warning" else "primary"
)
})
}
shinyApp(ui, server)
The output is like this, with valueboxes of unequal height:
I am using bslib v0.5.0 and Shiny v1.7.4.1