try.seed: Generate random seeds to preview their effects

View source: R/interactive_programming.R

try.seedR Documentation

Generate random seeds to preview their effects

Description

This function picks a random seed, announces what that seed is, and then uses it to evaluate an expression. For example, if you are creating a network graph whose layout is calculated from randomly-chosen starting positions, try.seed() lets you run that plotting function over and over with a new seed each time, until you find a layout that you would like to keep.

Usage

try.seed(expr, seed = NULL)

Arguments

expr

(Expression) An expression.

seed

(Integer or NULL) If NULL, generate and set a new seed. If you provide a seed in this argument, that seed will be used and no new seed will be generated.

Details

This function will throw an error if you try to generate a new seed non-interactively (e.g. in an Rmarkdown document that is being knitted, or an R script that is being executed from the command line). This is because try.seed() changes the random seed, and that could affect the rest of your script in ways that you don't want. It is allowed to run non-interactively if you set the seed argument to a non-NULL number.

When you find a seed that you want to keep, you should copy it from the console and provide it as the seed argument.

Value

The evaluated expr.

Authors

Examples

## Not run: 
try.seed(runif(5))

## End(Not run)

#> Seed is: 1605125467
#> [1] 0.2582169 0.9739978 0.4126912 0.1326866 0.1336819
#>

## Not run: 
try.seed(runif(5),
         seed = 1605125467)  # The announced seed

## End(Not run)

#> [1] 0.2582169 0.9739978 0.4126912 0.1326866 0.1336819


DesiQuintans/desiderata documentation built on April 9, 2023, 5:43 a.m.