Advertisement
karstenw

bslib color-mode

Jan 29th, 2025 (edited)
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.09 KB | None | 0 0
  1.     library(shiny)
  2.     library(bslib)
  3.     library(titanic) # example data
  4.     library(vcd) # mosaic plot
  5.     library(khroma) # color palettes
  6.     library(grid) # grid.rect
  7.  
  8.     ui <- bslib::page_navbar(
  9.       title = "Dark Mode Demo",
  10.       theme = bslib::bs_theme(version=5),
  11.       bslib::nav_panel(
  12.         title = "Base Graphics",
  13.         bslib::card(
  14.           bslib::card_header("Titanic: \u00dcberlebenswahrscheinlichkeit nach Klasse"),
  15.           shiny::plotOutput("base_mosaic")
  16.         )
  17.       ),
  18.       bslib::nav_spacer(),
  19.       bslib::nav_item(
  20.         bslib::input_dark_mode(id = "theme_toggle", mode = "light")
  21.       )
  22.     )
  23.  
  24.     server <- function(input, output, session) {
  25.       # dark / light mode
  26.       shiny::observeEvent(input$theme_toggle, {
  27.         bslib::toggle_dark_mode(mode=input$theme_toggle)
  28.       })
  29.  
  30.       # load only once
  31.       titanic_dat <- shiny::reactive(
  32.         titanic::titanic_train
  33.       )
  34.      
  35.       output$base_mosaic <- shiny::renderPlot({
  36.         current_theme <- bslib::bs_current_theme()
  37.         if(input$theme_toggle=="dark") {
  38.             bg <- bslib::bs_get_variables(current_theme, "body-bg-dark")[["body-bg-dark"]]
  39.             fg <- bslib::bs_get_variables(current_theme, "body-color-dark")[["body-color-dark"]]
  40.             clr <- khroma::color("dark")(2)
  41.         } else {
  42.             bg <- bslib::bs_get_variables(current_theme, "body-bg")[["body-bg"]]
  43.             fg <- bslib::bs_get_variables(current_theme, "body-color")[["body-color"]]
  44.             clr <- khroma::color("light")(2)
  45.         }
  46.        
  47.         # base mosaicplot does not allow changing the color of the variable names, so we use vcd::mosaic
  48.         # vcd::mosaic does not allow changing the background color, so we use grid.rect
  49.         # vcd::mosaic does not allow to change the title color, so we don't use the main argument
  50.         grid::grid.newpage()
  51.         grid::grid.rect(gp = grid::gpar(fill = bg, col = NA))  
  52.         vcd::mosaic(
  53.           formula=as.formula("~ Survived + Pclass"),
  54.           data = titanic_dat(),
  55.           gp = grid::gpar(col = fg, fill=clr),
  56.           labeling_args = list(
  57.             gp_labels = grid::gpar(col = fg),  
  58.             gp_varnames = grid::gpar(col = fg)
  59.           ),
  60.           newpage=FALSE # since we used grid.newpage() already
  61.         )
  62.        
  63.       })
  64.     }
  65.  
  66.     shiny::shinyApp(ui, server)
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement