From sfalcon at fhcrc.org  Thu Jan  4 00:21:18 2007
From: sfalcon at fhcrc.org (Seth Falcon)
Date: Wed, 03 Jan 2007 15:21:18 -0800
Subject: [Rd] Capturing argument values
In-Reply-To: <20061231012017.GD23253@wheat.betterworld.us> (Ross Boylan's
	message of "Sat, 30 Dec 2006 17:20:17 -0800")
References: <20061231012017.GD23253@wheat.betterworld.us>
Message-ID: <m27iw3d7dd.fsf@fhcrc.org>

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



[Rd] Capturing argument values
Seth Falcon