Give a common missing argument in functional

I have a set of small functions that generate random strings similar to rnorm or sample . All these functions have common arguments, for simplicity we can say that one common argument is n . I would like to make a large function (functional) that takes n as and the argument plus ... , which can be any of the small functions. This meta-function evaluates small functions using the set n if they have this argument. here is an example:

## LITTLE FUNCTIONS

 fun1 <- function(n, x = 1:10) sample(x, n, TRUE) fun2 <- function(n, x = LETTERS) sample(x, n, TRUE) fun3 <- function(n, x = 50) rnorm(n, x) fun4 <- function(n, x = 100, y = 10) rnorm(n, x, y) 

FUNCTIONAL (META FUNCTION)

 combiner <- function(n, ...){ ## Where the magic needs to happen. Set n for `fun1` `fun2` and `fun4` ## evaluate all these functions } ## Here we pass `n = 6` combiner( 6, fun1(), fun2, rnorm(), fun4(y=8) ) 

I would like it to evaluate functions even if they are missing () , as is the case with fun2 above, but this is more suitable. I think this is possible because magrittr pipes can do this .

## DESIRED EXIT

 list( fun1(6), fun2(6), rnorm(6), fun4(6, y=8) ) ## OUTPUT IS SEED DEPENDANT ## [[1]] ## [1] 2 1 6 6 1 1 ## ## [[2]] ## [1] "V" "Z" "A" "F" "F" "G" ## ## [[3]] ## [1] -0.91932716 -0.05833169 1.75440750 2.19959565 -0.11145315 1.32216601 ## ## [[4]] ## [1] 107.48747 89.55798 93.15771 111.32380 100.82104 104.07829 
+5
source share
1 answer

This is how I approach this:

 combiner <- function(n, ...) { ## Capture the unevaluated calls and symbols passed via ... ll <- as.list(substitute(list(...)))[-1] ## Process each one in turn lapply(ll, FUN = function(X) { ## Turn any symbols/names into calls if(is.name(X)) X <- as.call(list(X)) ## Add/replace an argument named n X$n <- n ## Evaluate the fixed up call eval(X) }) } combiner(6, fun1(), fun2, rnorm(), fun4(y=8)) # [[1]] # [1] 3 8 9 7 4 7 # # [[2]] # [1] "Z" "M" "U" "A" "Z" "U" # # [[3]] # [1] 0.6100340 -1.0323017 -0.6895327 1.2534378 -0.3513120 0.3116020 # # [[4]] # [1] 112.31979 91.96595 79.11932 108.30020 107.16828 89.46137 
+6
source

All Articles