[Rd] Capturing argument values

Ross Boylan ross at biostat.ucsf.edu
Sun Dec 31 02:20:17 CET 2006

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?

I realize I could capture the function's entire local frame, but that
has quite a bit of stuff I don't want in it.  I suspect some of the
items in it might be promises, and so would not have the values I
needed as well.  (Also the frame could later change, though I guess I
could convert it to a list to avoid that problem.)

Ross Boylan

More information about the R-devel mailing list