[Rd] Capturing argument values

From: Ross Boylan <ross_at_biostat.ucsf.edu>
Date: Sun 31 Dec 2006 - 01:20:17 GMT


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



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sun Dec 31 12:23:04 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 04 Jan 2007 - 01:31:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.