Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(shiny)
- library(bslib)
- library(titanic) # example data
- library(vcd) # mosaic plot
- library(khroma) # color palettes
- library(grid) # grid.rect
- ui <- bslib::page_navbar(
- title = "Dark Mode Demo",
- theme = bslib::bs_theme(version=5),
- bslib::nav_panel(
- title = "Base Graphics",
- bslib::card(
- bslib::card_header("Titanic: \u00dcberlebenswahrscheinlichkeit nach Klasse"),
- shiny::plotOutput("base_mosaic")
- )
- ),
- bslib::nav_spacer(),
- bslib::nav_item(
- bslib::input_dark_mode(id = "theme_toggle", mode = "light")
- )
- )
- server <- function(input, output, session) {
- # dark / light mode
- shiny::observeEvent(input$theme_toggle, {
- bslib::toggle_dark_mode(mode=input$theme_toggle)
- })
- # load only once
- titanic_dat <- shiny::reactive(
- titanic::titanic_train
- )
- output$base_mosaic <- shiny::renderPlot({
- current_theme <- bslib::bs_current_theme()
- if(input$theme_toggle=="dark") {
- bg <- bslib::bs_get_variables(current_theme, "body-bg-dark")[["body-bg-dark"]]
- fg <- bslib::bs_get_variables(current_theme, "body-color-dark")[["body-color-dark"]]
- clr <- khroma::color("dark")(2)
- } else {
- bg <- bslib::bs_get_variables(current_theme, "body-bg")[["body-bg"]]
- fg <- bslib::bs_get_variables(current_theme, "body-color")[["body-color"]]
- clr <- khroma::color("light")(2)
- }
- # base mosaicplot does not allow changing the color of the variable names, so we use vcd::mosaic
- # vcd::mosaic does not allow changing the background color, so we use grid.rect
- # vcd::mosaic does not allow to change the title color, so we don't use the main argument
- grid::grid.newpage()
- grid::grid.rect(gp = grid::gpar(fill = bg, col = NA))
- vcd::mosaic(
- formula=as.formula("~ Survived + Pclass"),
- data = titanic_dat(),
- gp = grid::gpar(col = fg, fill=clr),
- labeling_args = list(
- gp_labels = grid::gpar(col = fg),
- gp_varnames = grid::gpar(col = fg)
- ),
- newpage=FALSE # since we used grid.newpage() already
- )
- })
- }
- shiny::shinyApp(ui, server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement