From ce00124ad0b62cb69516c89c66ac72b72c277612 Mon Sep 17 00:00:00 2001 From: seb09 Date: Sun, 3 Nov 2024 22:48:22 +0100 Subject: [PATCH 1/3] Use `withr::with_seed()` in `random_id()` Addition or change of position of a `gt` should not change the seed of the random number generator in the global environment. Fixes #1915 --- DESCRIPTION | 4 ++-- R/helpers.R | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 1dbd19f86b..8fcd49ec77 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -51,6 +51,7 @@ Imports: scales (>= 1.3.0), tidyselect (>= 1.2.1), vctrs, + withr, xml2 (>= 1.3.6) Suggests: digest (>= 0.6.31), @@ -70,8 +71,7 @@ Suggests: shiny (>= 1.9.1), testthat (>= 3.1.9), tidyr, - webshot2 (>= 0.1.0), - withr + webshot2 (>= 0.1.0) Config/Needs/coverage: officer Config/Needs/website: quarto ByteCompile: true diff --git a/R/helpers.R b/R/helpers.R index db82ce6af9..050c532261 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -2926,7 +2926,19 @@ cell_style_structure <- function(name, obj, subclass = name) { #' @export random_id <- function(n = 10) { - paste(sample(letters, n, replace = TRUE), collapse = "") + # use last 5 digits of system time as seed + random_id_seed <- Sys.time() |> + as.numeric() |> + as.character() |> + # remove decimal point + sub("\\.", "", x = _) |> + # select last 5 digits + sub("\\d*(\\d{5}$)", "\\1", x = _) |> + as.numeric() + + withr::with_seed(random_id_seed, { + paste(sample(letters, n, replace = TRUE), collapse = "") + }) } latex_special_chars <- c( From f329fddc905549950c65a891bed356a111e0ded4 Mon Sep 17 00:00:00 2001 From: seb09 Date: Sun, 3 Nov 2024 23:07:31 +0100 Subject: [PATCH 2/3] add test and NEWS --- NEWS.md | 2 ++ tests/testthat/test-helpers.R | 14 ++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 tests/testthat/test-helpers.R diff --git a/NEWS.md b/NEWS.md index 60e307526d..b8ebceec77 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # gt (development version) +* The `random_id()` function now relies on `withr::with_seed()` and no longer affects the seed of the random number generator in the global environment. + # gt 0.11.1 ## Breaking changes diff --git a/tests/testthat/test-helpers.R b/tests/testthat/test-helpers.R new file mode 100644 index 0000000000..82bb887ad5 --- /dev/null +++ b/tests/testthat/test-helpers.R @@ -0,0 +1,14 @@ +test_that("random_id() does not affect random seed", { + + withr::with_seed(123, { + r1 <- runif(1) + }) + + withr::with_seed(123, { + id <- random_id() + r2 <- runif(1) + }) + + expect_equal(r1, r2) + +}) From cbb0de6a3430a9f2c8aaf116cb2cbda1857272a7 Mon Sep 17 00:00:00 2001 From: seb09 Date: Mon, 4 Nov 2024 18:46:54 +0100 Subject: [PATCH 3/3] typo --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index b8ebceec77..b30d8ad165 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # gt (development version) -* The `random_id()` function now relies on `withr::with_seed()` and no longer affects the seed of the random number generator in the global environment. +* The `random_id()` function now relies on `withr::with_seed()` and no longer affects the state of the random number generator in the global environment. # gt 0.11.1