+ - 0:00:00
Notes for current slide
Notes for next slide

Shiny v1.6 overview: improvements to caching and theming

Carson Sievert, Software Engineer @ RStudio

carson@rstudio.com

1 / 16

Shiny v1.6 major features

  • New shiny::bindCache() function

    • Persistent caching for any reactive() or render*() function
  • Improved theming support

    • All of Shiny may now be (properly) themed via new {bslib} package
  • Accessibility improvements and many bug fixes

v1.6 is out now, blog post coming this week:

install.packages("shiny")
2 / 16

bindCache() in a nutshell

  • Similar to renderCachedPlot(), bindCache() needs the input(s) to a "pure" calculation:
r <- reactive({
input$x + input$y
}) %>%
bindCache(input$x, input$y)
3 / 16

bindCache() in a nutshell

  • Similar to renderCachedPlot(), bindCache() needs the input(s) to a "pure" calculation:
r <- reactive({
input$x + input$y
}) %>%
bindCache(input$x, input$y)
  • Should work with most existing render*() functions (some might need updates):
output$p <- renderPlotly({
plot_ly(dat) %>%
filter(city %in% input$cities) %>%
add_lines(time, population)
}) %>%
bindCache(input$cities)
4 / 16

bindCache() in a nutshell

  • Similar to renderCachedPlot(), bindCache() needs the input(s) to a "pure" calculation:
r <- reactive({
input$x + input$y
}) %>%
bindCache(input$x, input$y)
  • Should work with most existing render*() functions (some might need updates):
output$p <- renderPlotly({
plot_ly(dat) %>%
filter(city %in% input$cities) %>%
add_lines(time, population)
}) %>%
bindCache(input$cities)
shinyOptions(cache = cachem::cache_disk("./cache"))
5 / 16

Start using {bslib} with Shiny

library(shiny)
library(bslib)
ui <- fluidPage(
theme = bs_theme(),
...
)
  • fluidPage(), navbarPage(), bootstrapPage(), etc. all have this theme argument, which now accepts bslib::bs_theme() objects.
6 / 16

By default, upgrades app from Bootstrap 3 to 4

library(shiny)
library(bslib)
ui <- fluidPage(
theme = bs_theme(version = 4),
...
)
  • Special compatibility layer helps most Shiny apps & R Markdown docs upgrade to Bootstrap 4.

  • Upgrading may break some apps (try version = 3 in that case).

7 / 16

Continue using Bootswatch themes

library(shiny)
library(bslib)
# In the past, this was shinythemes::shinythemes("darkly")
ui <- fluidPage(
theme = bs_theme(bootswatch = "darkly"),
...
)
  • Now you can use Bootswatch with BS4 or BS3 (just change version).
    • Bootswatch 4 has some new themes (e.g., solar and minty)
8 / 16

Custom theming!

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

9 / 16

font_google(), by default, tries to download, cache, and bundle the font files in one shot.

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

10 / 16

How does custom theming actually work?

  • Shiny will compile Sass -> CSS at run-time when necessary.
    • Can be costly, but options(sass.cache) is TRUE by default, and we default to app sub-directory on hosted platforms (see sass::sass_cache_context_dir() for details).
11 / 16

Bootstrap Sass provides 100s of variables!

Main colors (e.g., bg, fg, etc) "cascade" to 100s of other settings, all of which can be set via Bootstrap Sass variables (e.g., $progress-bar-bg)

bs_theme(
bg = "#002B36", fg = "#EEE8D5",
"progress-bar-bg" = "orange"
)

We're currently working on searchable table(s) of variables https://github.com/rstudio/bslib/pull/238

12 / 16

Use with rmarkdown::html_document

---
output:
html_document:
theme:
bg: "#202123"
fg: "#B8BCC2"
primary: "#EA80FC"
base_font: !expr bslib::font_google("Grandstander")
---
  • Currently requires dev version of R Markdown remotes::install_github("rstudio/rmarkdown")

  • In theory, any output format building on html_document (or html_document_base) should automatically gain {bslib} support.

  • Some output formats (e.g. {flexdashboard}) need updates to restructure their harded-coded CSS to Sass.

13 / 16

Use real-time theming w/ any Shiny app

Call bs_themer() in the server code (also works with a runtime: shiny R Markdown doc)

14 / 16

Implement your own theming widgets w/ setCurrentTheme()

ui <- fluidPage(
theme = bs_theme(),
checkboxInput("dark_mode", "Dark mode", FALSE)
)
server <- function(input, output, session) {
observe(session$setCurrentTheme({
if (input$dark_mode) {
bs_theme(bg = "black", fg = "white", primary = "purple")
} else {
bs_theme()
}
}))
}
shinyApp(ui, server)

15 / 16

Thanks! Any questions?

Slides https://talks.cpsievert.me/20210126

16 / 16

Shiny v1.6 major features

  • New shiny::bindCache() function

    • Persistent caching for any reactive() or render*() function
  • Improved theming support

    • All of Shiny may now be (properly) themed via new {bslib} package
  • Accessibility improvements and many bug fixes

v1.6 is out now, blog post coming this week:

install.packages("shiny")
2 / 16
Paused

Help

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