View source: R/DynamicRNGFunction.R
| DynamicRNGFunction | R Documentation |
This function may be useful to advanced users of TrialSimulator. It
creates a wrapper function of a random number generator, while fixing a
subset or all of arguments. This function is design to prevent inadvertent
changing to arguments of random number generator. See examples below.
DynamicRNGFunction(fn, ...)
fn |
random number generator, e.g., |
... |
arguments for |
a function to generate random number based on fn and arguments in
.... Specified arguments will be fixed and cannot be changed when invoking
DynamicRNGFunction(fn, ...)(). For example,
if foo <- DynamicRNGFunction(rnorm, sd = 2),
then foo(n = 100) will always generate data from normal distribution of
variance 4. foo(n = 100, sd = 1) will trigger an error. However,
if an argument is not specified in DynamicRNGFunction, then it can be specified
later. For example, foo(n = 100, mean = -1) will generate data from N(-1, 4).
# example code
dfunc <- DynamicRNGFunction(rnorm, sd = 3.2)
x <- dfunc(1e3) # mean 0 and sd 3.2
hist(x)
y <- dfunc(1e3, mean = 3.5) # mean can be changed
mean(y)
try(z <- dfunc(1e3, sd = 1)) # error because sd is fixed in dfunc
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.