I need to use replicate
inside a custom function (the details are not important but it involves random number generation.
Let's say I'm using: rnorm(2)
I know I can repeat the expression 10 times using replicate(10, rnorm(2))
Now, I want to be able to use replicate
inside a function since I need to post-process the output and I also want to be able to change the replicated expression, so I'm trying something like this:
fn <- function(expression) replicate(10, expression)
But when I run the function I get the exact same numbers repeated 10 times:
fn(rnorm(2))
I know that fn
is getting the result of rnorm(2)
and that's why it is repeating the numbers so I need to tell fn
not to evaluate expression
. I've been digging into this and it seems I need to use substitute
inside fn
but I can't make it work.