set_seed: A 'set.seed'-wrapper to simplify reproducible coding.

Description Usage Arguments Details Value Examples

View source: R/set_seed.R

Description

A set.seed-wrapper to simplify reproducible coding.

Usage

1
2
3
set_seed(seed, expr = NULL, envir = parent.frame(), kind = NULL,
  normal.kind = NULL, vstr = NULL, create_kind_vstr_list = FALSE,
  create_code = FALSE)

Arguments

seed

The integer to be used as the seed value. This argument will be given to the ordinary set.seed-function.

expr

An expression, default NULL, to be evaluated after the seed has been set. The evaluation will be within the frame given by the three arguments kind, normal.kind and vstr, and thus gives an extra level of protection for those cases where the internal workings of an expression use set.seed with a fixed value for the seed. Note that the seed and the evaluation will be performed at the level of the calling function.

envir

The environment in which expr is to be evaluated, default value parent.frame(). See the documentation of eval for further details.

kind

Character or NULL. If kind is a character string, set R's RNG to the kind desired.

normal.kind

Character string or NULL. If it is a character string, set the method of Normal generation.

vstr

A character string containing a version number of R. The default value NULL will use the present version number. Note that the function RNGversion works as a wrapper that calls RNGkind based on the version number, and that it can be conflicts between the settings specified by vstr and the setting specified by kind and normal.kind. The settings indicated by vstr will be used if kind and normal.kind both are NULL, but if one or both of them are given, then settings will be based upon them.

create_kind_vstr_list

Logical value, default FALSE. This can be used to create a list with the values for kind, normal.kind and vstr. If this is used when the before mentioned arguments are NULL, then the present values from the active R-session will be stored. This can be used by functions that need to store detailed information in an updated call, to ensure reproducibility later on. This argument will be ignored if create_code is TRUE.

create_code

Logical value, default FALSE. This can be used to create a chunk of code to be inserted into e.g. a script. The idea is that this should be done when the script is created, in order to ensure that the present settings of the system is properly recorded for reproducible results if the script is to be used later on by other machines, versions of R.

Details

This function wraps around the function set.seed, RNGversion-function such that all the details related to kind, normal.kind and vstr are fixed by this single function. Moreover, it's also possible to return the code for calling this function (with the default values used) in order to ensure that computational scripts properly reflect the settings under which they initially were used. Finally, it's also possible to use this function to create a list of the present settings, such that these can be integrated into call of functions that are stored in order to ensure reproducibility. See the help-page of Random for details about the three arguments kind, normal.kind and vstr.

Value

The result depends on the value given to create_code. It will either be a setting of the seed, or it will be the code needed to set the seed. NOTE: The function will ensure that the settings for kind and normal.kind will be reverted to the original ones. The value of the .Random.seed-vector will also depend upon this. If kind and normal.kind was different from the existing values, the value of .Random.seed will be reverted back to what it was when the function was called. Otherwise, the Random.seed-vector will be similar to what it would have been if set.seed and the expression expr had been evaluated in the workspace.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
##  Gives the same effect as 'set.seed'.
set.seed(seed = 1)
rnorm(3)
.a <- .Random.seed
set_seed(seed = 1)
rnorm(3)
.b <- .Random.seed
identical(.a, .b)
## The 'expr'-argument is evaluated in the global environment,
## unless otherwise specified by the 'envir'-argument.
set_seed(seed = 1,
         expr = {.tmp <- rnorm(3)})
.tmp
.c <- .Random.seed
identical(.a, .c)

## No footprint on workspace when a change of RNG has been used.
set.seed(NULL)
.a <- .Random.seed
set_seed(seed =1,
         expr = {.tmp <- rnorm(3)},
         vstr = "1.0.0")
.tmp
.b <- .Random.seed
identical(.a, .b)

##  No footprint on workspace when a list is produced.
set.seed(NULL)
.a <- .Random.seed
set_seed(seed =1,
         expr = {.tmp <- rnorm(3)},
         vstr = "1.0.0",
         create_kind_vstr_list = TRUE)
.tmp
.b <- .Random.seed
identical(.a, .b)

## No footprint on workspace when code is produced.
set.seed(NULL)
.a <- .Random.seed
.seed <- 1
set_seed(seed = .seed,
         expr = {.tmp__ <- rnorm(3)},
         vstr = "1.0.0",
         create_code = TRUE)
.b <- .Random.seed
identical(.a, .b)
## Note: The creation of the code does not evaluate
## the 'expr'-argument.

LAJordanger/leanRcoding documentation built on Feb. 27, 2020, 4:42 p.m.