From f93c6dbcaa1c5f90790e8159b1387b999e8fe8ce Mon Sep 17 00:00:00 2001 From: Salim B Date: Wed, 18 Oct 2023 18:48:38 +0200 Subject: [PATCH] Set RNG seed for htmlwidgets IDs (#2294) `htmlwidgets::setWidgetIdSeed()` needs to be explicitly called for this due to how `htmlwidgets:::createWidgetId()` works --- NEWS.md | 1 + R/build-reference.R | 7 +++++-- man/build_reference.Rd | 4 ++-- man/build_site.Rd | 4 ++-- vignettes/test/widgets.Rmd | 2 +- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/NEWS.md b/NEWS.md index 015e838b00..0a1ea665d0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/build-reference.R b/R/build-reference.R index ef8c94e53d..add462c13a 100644 --- a/R/build-reference.R +++ b/R/build-reference.R @@ -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()]. @@ -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)) { diff --git a/man/build_reference.Rd b/man/build_reference.Rd index 81561844a4..730c2e2a36 100644 --- a/man/build_reference.Rd +++ b/man/build_reference.Rd @@ -31,8 +31,8 @@ rapidly prototype. It is set to \code{FALSE} by \code{\link[=build_site]{build_s \item{run_dont_run}{Run examples that are surrounded in \\dontrun?} -\item{seed}{Seed used to initialize so that random examples are -reproducible.} +\item{seed}{Seed used to initialize random number generation so that +examples are reproducible.} \item{override}{An optional named list used to temporarily override values in \verb{_pkgdown.yml}} diff --git a/man/build_site.Rd b/man/build_site.Rd index cb56aedb00..7b5e80af7d 100644 --- a/man/build_site.Rd +++ b/man/build_site.Rd @@ -25,8 +25,8 @@ build_site( \item{run_dont_run}{Run examples that are surrounded in \\dontrun?} -\item{seed}{Seed used to initialize so that random examples are -reproducible.} +\item{seed}{Seed used to initialize random number generation so that +examples are reproducible.} \item{lazy}{If \code{TRUE}, will only rebuild articles and reference pages if the source is newer than the destination.} diff --git a/vignettes/test/widgets.Rmd b/vignettes/test/widgets.Rmd index aee3699602..f4200cc90d 100644 --- a/vignettes/test/widgets.Rmd +++ b/vignettes/test/widgets.Rmd @@ -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) }