[R] how to pass extra parameters using call() or similar mechanism ?
Tony Plate
tplate at blackmesacapital.com
Fri Mar 12 01:23:44 CET 2004
If you want to use call() with a list of arguments, the easiest way I know
of is to use do.call() (if anyone knows a better way, please say so!)
> myfun2 <- function(method, x, ...) {
+ method.call <- do.call("call", list(method, x, ...))
+ eval(method.call)
+ }
> myfun2('sqrt',2)
[1] 1.414214
>
If you don't need the call object, you can just use do.call() directly
(this is the same as Andy Liaw's suggestion):
> myfun <- function(method, x, ...) {
+ do.call(method, list(x, ...))
+ }
> myfun('sqrt',2)
[1] 1.414214
At Thursday 01:03 PM 3/11/2004, ryszard.czerminski at pharma.novartis.com wrote:
>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
More information about the R-help
mailing list