{bslib} & {thematic}{bslib}: tools for styling HTML from R (e.g., Shiny apps, rmarkdown::html_document(), etc)
{thematic}: simplified theming of R plots ({ggplot2}, {lattice}, & {base})
Not yet on CRAN (give it about a month or so)
remotes::install_github(c("rstudio/bslib", "rstudio/thematic")){bslib} with Shinylibrary(shiny)library(bslib)ui <- fluidPage( theme = bs_theme(), ...)
fluidPage(), navbarPage(), bootstrapPage(), etc. all have this theme argument.
You may already be using theme with {shinythemes} or your own custom Bootstrap CSS.
bs_theme() is way more powerful!library(shiny)library(bslib)ui <- fluidPage( theme = bs_theme(version = "4+3"), ...)
bs_theme() defaults to version = "4+3", which means BS4 plus added compatibility for BS3. version = 3.library(shiny)library(bslib)# In the past, this was shinythemes::shinythemes("darkly")ui <- fluidPage( theme = bs_theme(bootswatch = "darkly"), ...)
version).
bs_theme_preview( bs_theme(bootswatch = "darkly"))

bs_theme_preview(bs_theme( bg = "black", fg = "white", primary = "red", base_font = "Grandstander"))

bs_theme_preview(bs_theme( bg = "#202123", fg = "#B8BCC2", primary = "#EA80FC", base_font = "Grandstander"))

bs_theme() expectations"Just works" with:
"Just works" soon with:
navbarPage(), sliderInput(), etc)rmarkdown::html_document(), DT::datatable(), other popular {htmlwidgets}Can't work with things not rendered by web browser (e.g., plotOutput()).
{thematic} to "translate" CSS to R plots!bs_theme() 😭fluidPage( theme = bs_theme(bg = "#002B36", fg = "#EEE8D5", primary = "#2AA198", base_fonts = "Pacifico"), tabsetPanel(type = "pills", tabPanel("ggplot", plotOutput("ggplot")), tabPanel("lattice", plotOutput("lattice")), tabPanel("base", plotOutput("base"))))

{thematic} to the rescue! 🎉thematic::thematic_shiny()fluidPage( theme = bs_theme(bg = "#002B36", fg = "#EEE8D5", primary = "#2AA198", base_fonts = "Pacifico"), tabsetPanel(type = "pills", tabPanel("ggplot", plotOutput("ggplot")), tabPanel("lattice", plotOutput("lattice")), tabPanel("base", plotOutput("base"))))

{thematic} package, in general{thematic} alters R plotting defaults using a few simple settings. Use thematic_on() to enable globally (until thematic_off() is called).library(thematic)library(ggplot2)thematic_on( bg = "black", fg = "white", accent = "red", font = "Indie Flower")ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_smooth()

{thematic} package, in general{thematic} alters R plotting defaults using a few simple settings. Use thematic_on() to enable globally (until thematic_off() is called).library(thematic)library(ggplot2)thematic_on( bg = "black", fg = "white", accent = "red", font = "Indie Flower")ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_smooth()
Since Indie Flower is a Google Font, {thematic} installs it if needed!

{thematic}"auto"-detected!library(thematic)library(ggplot2)thematic_on( bg = "auto", fg = "auto", accent = "auto", font = "auto")ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_smooth()

{thematic}"auto"-detected!library(thematic)library(ggplot2)thematic_on( bg = "auto", fg = "auto", accent = "auto", font = "auto")ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_smooth()
"auto" works best via shiny::renderPlot() (works with any CSS, not just bs_theme())!

{thematic}"auto"-detected!library(thematic)library(ggplot2)thematic_on( bg = "auto", fg = "auto", accent = "auto", font = "auto")ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_smooth()
"auto" can work with bs_theme() in rmarkdown::html_document()

{thematic}"auto"-detected!library(thematic)library(ggplot2)thematic_on( bg = "auto", fg = "auto", accent = "auto", font = "auto")ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_smooth()
"auto" detects your RStudio Theme inside RStudio

{thematic}, back to {bslib}BTW, {thematic} also makes it easy to control qualitative and sequential colorscales.
# Includes `bs_themer()`, an interactive widget for real-time theming!bs_theme_preview(bs_theme(bg = "#202123", fg = "#B8BCC2", primary = "#EA80FC", base_font = "Grandstander"))
$progress-bar-bg)bs_theme( bg = "#002B36", fg = "#EEE8D5", "progress-bar-bg" = "orange")

Beware, Sass variables can be quite different across versions!
bs_theme() sets Bootstrap Sass variables (CSS compilation happens magically at run-time) Learn more about Sass and the {sass} 📦 at https://rstudio.github.io/sass
Customize spacing, borders, modify colors, and more!
tabsetPanel( tabPanel("One", "No padding"), tabPanel("Two", "No padding"))

tabsetPanel( tabPanel( "One", "With padding", class = "p-3 border border-top-0 rounded-bottom" ), tabPanel("Two", "No padding"))

See more examples at https://rstudio.github.io/bslib/articles/recipes.html
Add Sass rules to do things like @extend all navs to be centered
fluidPage( theme = bs_theme() %>% bs_add_rules(".nav { @extend .justify-content-center; }"), tabsetPanel( tabPanel("One", "Centered w/ padding", class = "p-3 border border-top-0 rounded-bottom"), tabPanel("Two", "No padding") ))

See more examples at https://rstudio.github.io/bslib/articles/recipes.html
person <- function(name, title, company) { div( class = "person", h3(class = "name", name), div(class = "title", title), div(class = "company", company) )}fluidPage( person("Andrew Carnegie", "Owner", "Carnegie Steel Company"), person("John D. Rockefeller", "Chairman", "Standard Oil"), theme = bs_theme(bg = "#002B36", fg = "#EEE8D5") %>% bs_add_rules(sass::sass_file("person.scss"))).person { display: inline-block; padding: $spacer; border: $border-width solid $border-color; @include border-radius; @include box-shadow; outline: 0; width: 300px; @include media-breakpoint-down(sm) { display: block; width: auto; margin-right: $grid-gutter-width; } margin: $grid-gutter-width; margin-right: 0; .title { font-weight: bold; } .title, .company { color: $gray-600; }}.person:last-of-type { margin-right: $grid-gutter-width;}
setCurrentTheme()!dark <- bs_theme(bg = "black", fg = "white")light <- bs_theme()ui <- fluidPage( theme = light, checkboxInput("dark_mode", "Dark mode", FALSE))server <- function(input, output, session) { observe(session$setCurrentTheme( if (input$dark_mode) dark else light ))}shinyApp(ui, server)

Very new and experimental (learn more)!
Pass bs_theme() parameters to html_document() and html_document_base()1 (we hope to extend this to other output formats as well).
--- output: html_document: theme: bg: "#202123" fg: "#B8BCC2" primary: "#EA80FC" base_font: "Grandstander"---
remotes::install_github("rstudio/rmarkdown#1706")Customize the bs_theme() further with bs_global_*()
--- output: html_document: theme: bg: "#202123" fg: "#B8BCC2" primary: "#EA80FC" base_font: "Grandstander"--- ```{r, echo = FALSE}library(bslib)bs_global_add_rules(".nav { @extend .justify-content-center; }")```
remotes::install_github("rstudio/rmarkdown#1706")Use {bslib} to style HTML in Shiny & R Markdown.
version = 3).bs_theme().bs_add_rules()@extending Utility Classes or creating your own Bootstrap Sass styles! Use {thematic} for easier theming of R plots
{ggplot2}, plotly::ggplotly(), {lattice}, and {base} R graphics.{thematic}. @cpsievert
@cpsievert
cpsievert1@gmail.com
https://cpsievert.me/
Slides made possible thanks to {xaringan} and {xaringanExtra}
{bslib}: tools for styling HTML from R (e.g., Shiny apps, rmarkdown::html_document(), etc)
{thematic}: simplified theming of R plots ({ggplot2}, {lattice}, & {base})
Not yet on CRAN (give it about a month or so)
remotes::install_github(c("rstudio/bslib", "rstudio/thematic"))Keyboard shortcuts
| ↑, ←, Pg Up, k | Go to previous slide |
| ↓, →, Pg Dn, Space, j | Go to next slide |
| Home | Go to first slide |
| End | Go to last slide |
| Number + Return | Go to specific slide |
| b / m / f | Toggle blackout / mirrored / fullscreen mode |
| c | Clone slideshow |
| p | Toggle presenter mode |
| t | Restart the presentation timer |
| ?, h | Toggle this help |
| Esc | Back to slideshow |