r - Add string of (variable number of) arguments to a function within a function -


let's have function creates x number of objects based on length of input variable. want x number of objects used arguments in function supply function. assumign number of arguments variable (based on number of argument names provided), how can this?

can via string of argument names perhaps?

a non-working example illustrate i'm asking:

(in case, using arguments created outside function simplify example):

foo <- 1:5 na.rm <- t func <- mean  f1 <- function(func,arg.names) {    func(get(arg.names)) } f1(func,arg.names = c('foo','na.rm') 

how do in way get's arguments list?

we can try mget

f1 <- function(func,arg.names) {        lst <- mget(arg.names, envir = parent.frame())         func(lst[[1]], na.rm = lst[[2]])  }  f1(func, arg.names = c('foo', 'na.rm')) #[1] 3 

or option do.call (as mentioned in @thelatemail's post) change name of first list element 'x' x 'data' argument in mean function

## default s3 method:

mean(x, trim = 0, na.rm = false, ...)

 f1 <- function(func,arg.names) {          lst <- mget(arg.names, envir = parent.frame())         names(lst)[1] <- 'x'          do.call(func, lst)      }  f1(func, arg.names = c('foo', 'na.rm')) #[1] 3 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -