Skip to content

Commit

Permalink
Follow up to #1706: When runtime:shiny is used with bslib, call shiny…
Browse files Browse the repository at this point in the history
…::bootstrapLib() inside html_document_base()'s preprocessor (instead of at run()-time)

The former time-point is too early -- it doesn't allow other output formats like flex_dashboard to modify the theme before passing it to html_document_base()
  • Loading branch information
cpsievert committed Feb 18, 2021
1 parent a62cb20 commit 727b32f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 36 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: rmarkdown
Type: Package
Title: Dynamic Documents for R
Version: 2.6.6
Version: 2.6.7
Authors@R: c(
person("JJ", "Allaire", role = "aut", email = "[email protected]"),
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
Expand Down
15 changes: 11 additions & 4 deletions R/html_document_base.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,17 @@ html_document_base <- function(theme = NULL,
format_deps <- append(format_deps, html_dependency_header_attrs())
if (!is.null(theme)) {
format_deps <- append(format_deps, list(html_dependency_jquery()))
format_deps <- append(format_deps, bootstrap_dependencies(
# If TRUE, an as_bs_theme(theme) has been set globally (so users may customize it)
if (is_bs_theme(theme)) bslib::bs_global_get() else theme
))
# theme was set globally pre-knit, and it may be modified during knit
if (is_bs_theme(theme)) {
theme <- bslib::bs_global_get()
}
bootstrap_deps <- if (is_bs_theme(theme) && is_shiny(runtime)) {
if (!is_available("shiny", "1.6.0")) stop("Using a {bslib} theme with `runtime: shiny` requires shiny 1.6.0 or higher.")
list(shiny::bootstrapLib(theme))
} else {
bootstrap_dependencies(theme)
}
format_deps <- append(format_deps, htmltools::resolveDependencies(bootstrap_deps))
}
else if (isTRUE(bootstrap_compatible) && is_shiny(runtime)) {
# If we can add bootstrap for Shiny, do it
Expand Down
17 changes: 5 additions & 12 deletions R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,13 @@ run <- function(file = "index.Rmd", dir = dirname(file), default_file = NULL,

# determine the runtime of the target file
target_file <- file %||% file.path(dir, default_file)
yaml_front <- if (length(target_file)) yaml_front_matter(target_file)
runtime <- yaml_front$runtime
theme <- render_args$output_options$theme
if (length(target_file)) {
format <- output_format_from_yaml_front_matter(read_utf8(target_file))
theme <- format$options$theme
}
runtime <- if (length(target_file)) yaml_front_matter(target_file)

# run using the requested mode
if (is_shiny_prerendered(runtime)) {

# get the pre-rendered shiny app
app <- shiny_prerendered_app(target_file, render_args = render_args, theme = theme)
app <- shiny_prerendered_app(target_file, render_args = render_args)
} else {

# add rmd_resources handler on start
Expand All @@ -158,7 +152,7 @@ run <- function(file = "index.Rmd", dir = dirname(file), default_file = NULL,
# combine the user-supplied list of Shiny arguments with our own and start
# the Shiny server; handle requests for the root (/) and any R markdown files
# within
app <- shiny::shinyApp(ui = rmarkdown_shiny_ui(dir, default_file, theme),
app <- shiny::shinyApp(ui = rmarkdown_shiny_ui(dir, default_file),
uiPattern = "^/$|^/index\\.html?$|^(/.*\\.[Rr][Mm][Dd])$",
onStart = onStart,
server = rmarkdown_shiny_server(
Expand Down Expand Up @@ -317,7 +311,7 @@ rmarkdown_shiny_server <- function(dir, file, auto_reload, render_args) {
}

# create the Shiny UI function
rmarkdown_shiny_ui <- function(dir, file, theme) {
rmarkdown_shiny_ui <- function(dir, file) {
function(req) {
# map requests to / to requests for the default--index.Rmd, or another if
# specified
Expand All @@ -339,8 +333,7 @@ rmarkdown_shiny_ui <- function(dir, file, theme) {
tags$div(
tags$head(
tags$script(src = "rmd_resources/rmd_loader.js"),
tags$link(href = "rmd_resources/rmd_loader.css", rel = "stylesheet"),
shiny_bootstrap_lib(theme)
tags$link(href = "rmd_resources/rmd_loader.css", rel = "stylesheet")
),

# Shiny shows the outer conditionalPanel as long as the document hasn't
Expand Down
20 changes: 1 addition & 19 deletions R/shiny_prerendered.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ shiny_prerendered_app <- function(input_rmd, render_args, theme) {

# get rendered html and capture dependencies
html <- shiny_prerendered_html(input_rmd, render_args)
deps <- c(
htmltools::htmlDependencies(html),
shiny_bootstrap_lib(theme)
)
deps <- htmltools::htmlDependencies(html)

# create the server environment
server_envir = new.env(parent = globalenv())
Expand Down Expand Up @@ -687,18 +684,3 @@ shiny_prerendered_data_file_name <- function(label, cache) {
type <- ifelse(cache, ".cached", "")
sprintf("%s%s.RData", label, type)
}

# Use me instead of html_dependency_bootstrap() in a shiny runtime to get
# dynamic theming (i.e., have it work with session$setCurrentTheme())
shiny_bootstrap_lib <- function(theme) {
theme <- resolve_theme(theme)
if (!is_bs_theme(theme)) {
return(NULL)
}
if (!is_available("shiny", "1.5.0.9007")) {
stop(
"Using a {bslib} theme with `runtime: shiny` requires shiny 1.5.0.9007 or higher."
)
}
shiny::bootstrapLib(theme)
}

0 comments on commit 727b32f

Please sign in to comment.