[R] how to pass extra parameters using call() or similar mech anism ?

Liaw, Andy andy_liaw at merck.com
Fri Mar 12 00:16:02 CET 2004


See if this helps:

> callfun <- function(FUN, x, ...) {
+   do.call(FUN, c(list(x), list(...)))
+ }
> x <- c(1, 2, 5, NA)
> callfun("sum", x)
[1] NA
> callfun("sum", x, na.rm=TRUE)
[1] 8

Andy

> From: ryszard.czerminski at pharma.novartis.com
> 
> I am trying to write a function, which would allow to call 
> various methods
> and would pass to them extra arbitrary parameters.
> My first attempt was to use call() as illustrated below, but 
> apparently
> '...' cannot be used in such context.
> 
> How can this be achieved ?
> 
> Best regards,
> 
> Ryszard
> 
> > myfun <- function(method, x, ...) {
> +   v <- eval(call(method, x, ...))
> + }
> > method = 'sqrt'
> > myfun('sqrt',2)
> Error in eval(call(method, x, ...)) : ... used in an incorrect context
> > eval(call(method, 2, ...))
> Error in eval(call(method, 2, ...)) : ... used in an incorrect context
> > eval(call(method, 2))
> [1] 1.414214
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}




More information about the R-help mailing list