[Rd] Capturing argument values

Seth Falcon sfalcon at fhcrc.org
Thu Jan 4 00:21:18 CET 2007


Ross Boylan <ross at biostat.ucsf.edu> writes:

> I would like to preserve the values of all the arguments to a function
> in a results object.
> foo <- function(a, b=1) ....
> foo(x, 3)
>
> match.call() looks promising, but it records that a is x, while I want
> the value of x (in the calling frame).  Also, if the invocation is
> foo(x), then match.call doesn't record that b is 1.
>
> So I tried this (inside the function definition):
>     myargs <- lapply(names(formals()),
>                      function(x) eval(as.name(x)))
>
> That's pretty close.  However, my function has an optional argument in
> this sense:
> bar <- function(x, testing) ...
> where code in the body is 
>   if (! missing(testing)) do stuff
>
> When the eval in the previous lapply runs for a function call in which
> testing is not supplied, I get
> Error in eval(expr, envir, enclos) : argument "testing" is missing,
> with no default
> exposing a weakness in both my implementation and problem specification.
>
> I think I could simply screen "testing" out of the formals and be
> happy, but are there better ways of handling this situation?

Why not just give testing a default value?  Otherwise, filtering out
missing args as you mentioned.

+ seth



More information about the R-devel mailing list