Skip to content

Commit

Permalink
Use withr::with_seed() in random_id()
Browse files Browse the repository at this point in the history
Addition or change of position of a `gt` should not change the seed of the random number generator in the global environment. Fixes rstudio#1915
  • Loading branch information
seb09 committed Nov 3, 2024
1 parent 8d1a021 commit ce00124
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Imports:
scales (>= 1.3.0),
tidyselect (>= 1.2.1),
vctrs,
withr,
xml2 (>= 1.3.6)
Suggests:
digest (>= 0.6.31),
Expand All @@ -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
Expand Down
14 changes: 13 additions & 1 deletion R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit ce00124

Please sign in to comment.