Re: [Rd] Capturing argument values

From: Seth Falcon <sfalcon_at_fhcrc.org>
Date: Wed 03 Jan 2007 - 23:21:18 GMT

Ross Boylan <ross@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



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu Jan 04 11:36:59 2007

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 - 09:31:01 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.