Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render_args passed to run() can contain theme in output_options #2065

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions R/html_dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ as_bs_theme <- function(theme) {
}

is_bs_theme <- function(theme) {
is_available("bslib") &&
bslib::is_bs_theme(theme)
is_available("bslib") && bslib::is_bs_theme(theme)
}

theme_version <- function(theme) {
Expand Down
6 changes: 4 additions & 2 deletions R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,16 @@ run <- function(file = "index.Rmd", dir = dirname(file), default_file = NULL,
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
# Let shiny::getCurrentTheme() know about the yaml's theme, so
# things like `bslib::bs_themer()` can work with prerendered documents.
# Also note that we add the actual shiny::bootstrapLib() dependency
# inside the document's pre-processing hook so the 'last' version of
# the theme wins out
if (length(target_file)) {
format <- output_format_from_yaml_front_matter(read_utf8(target_file))
format <- output_format_from_yaml_front_matter(
input_lines = read_utf8(target_file),
output_options = render_args$output_options
)
set_current_theme(resolve_theme(format$options$theme))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If no theme option is provided in the YAML header, and in render_args, this means that the format default will be used for rendering. However, output_format_from_yaml_front_matter won't resolved default value and just return what it in the YAML, so in the case describe above, format$options$theme will be NULL and resolve_theme() will return NULL, as if no theme.

Is this important for set_current_theme ? I believe it should it return the default value of the format instead, but not sure.

What do you think @cpsievert ?
If you tell me what you need, I'll find the way to make it using rmarkdown internal function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be extra paranoid, maybe we should also change this line in set_current_theme():

if (is.function(set_theme)) set_theme(theme)

to

if (is.function(set_theme) && is_bs_theme(theme)) set_theme(theme)

just to make sure other code doesn't get confused about a character string as the theme.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this in ff6c7b8. It required to rewrite the test as shinyOptions() was not reset between test and passing NULL or a character to set_current_theme would have not reset the previous one. I could have change the order of the test, but having a clean way of testing this is safer for later.

Regarding my comment above, it is answered currently as only bs theme object will be passed. It will always be passed by users in YAML or command arguments, so it will work.

However, if a bs theme is a default for a output format (which is not the case yet), I still think we may have an issue and the theme value won't be passed to shiny as you expect. We only catch user specified theme here, not default format theme. Is it catch elsewhere ?

I'll try to check that with a test case.

}

Expand Down
9 changes: 6 additions & 3 deletions tests/testthat/test-shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ test_that("file.path.ci returns correctly no matter the case", {
})


test_that("set_current_theme() informs shiny::getCurrentTheme()", {
test_that("set_current_theme() informs shiny::getCurrentTheme() only with bslib theme", {
skip_if_not(packageVersion("shiny") >= 1.6)
expect_null(shiny::getCurrentTheme())
theme <- bslib::bs_theme()
set_current_theme(theme)
set_current_theme(theme <- bslib::bs_theme())
expect_equal(theme, shiny::getCurrentTheme())
set_current_theme(NULL)
expect_null(shiny::getCurrentTheme())
set_current_theme(theme <- "cerulean")
expect_equal(theme, shiny::getCurrentTheme())
set_current_theme(NULL)
})

test_that("html_prerendered is a full document template to use as UI for shiny", {
Expand Down