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

Set RNG seed for htmlwidgets IDs #2294

Merged
merged 2 commits into from
Oct 18, 2023
Merged
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Remove redundant entries in the documentation index when multiple explicit `@usage` tags are provided (@klmr, #2302)
* The article index now sorts vignettes and non-vignette articles alphabetically by their filename (literally, their `basename()`), by default (@jennybc, #2253).
* Add Catalan translation (@jmaspons, #2333)
* Set RNG seed for htmlwidgets IDs. This reduces noise in pkgdown reference HTML output when examples generate htmlwidgets (@salim-b, #2294).

# pkgdown 2.0.7

Expand Down
7 changes: 5 additions & 2 deletions R/build-reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@
#' rapidly prototype. It is set to `FALSE` by [build_site()].
#' @param run_dont_run Run examples that are surrounded in \\dontrun?
#' @param examples Run examples?
#' @param seed Seed used to initialize so that random examples are
#' reproducible.
#' @param seed Seed used to initialize random number generation so that
#' examples are reproducible.
#' @param devel Determines how code is loaded in order to run examples.
#' If `TRUE` (the default), assumes you are in a live development
#' environment, and loads source package with [pkgload::load_all()].
Expand Down Expand Up @@ -223,6 +223,9 @@ examples_env <- function(pkg, seed = 1014, devel = TRUE, envir = parent.frame())
width <- purrr::pluck(pkg, "meta", "code", "width", .default = 80)
withr::local_options(width = width, .local_envir = envir)
withr::local_seed(seed)
if (requireNamespace("htmlwidgets", quietly = TRUE)) {
htmlwidgets::setWidgetIdSeed(seed)
}

examples_env <- child_env(globalenv())
if (file_exists(pre_path)) {
Expand Down
4 changes: 2 additions & 2 deletions man/build_reference.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/build_site.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vignettes/test/widgets.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Test spacing above widget.

```{r, echo=FALSE}
# set seed for reproducible widget id
if (requireNamespace("htmltools", quietly = TRUE)) {
if (requireNamespace("htmlwidgets", quietly = TRUE)) {
htmlwidgets::setWidgetIdSeed(42)
Copy link
Member

Choose a reason for hiding this comment

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

Can we drop this block now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nope. The htmlwidgets seed init in R/build-reference.R above only applies to htmlwidgets that are generated in examples (built by build_reference()), not vignettes/articles (built by build_articles()). Currently, only the former function offers a seed param.

I think we need to modify the envir passed to rmarkdown::render() if we also want to have stable htmlwidget IDs for articles by default:

envir = globalenv(),

Should I give it a try? If yes, in a separate PR?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, let's do that in a separate PR. I'll merge this one momentarily. Thanks for working on this!

}
Expand Down
Loading