From murdoch at stats.uwo.ca  Tue Mar  7 19:59:36 2006
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Tue, 07 Mar 2006 13:59:36 -0500
Subject: [Rd] Expanding partial names
In-Reply-To: <440DBE19.6070308@vanderbilt.edu>
References: <440D9295.7090207@stats.uwo.ca> <440DBE19.6070308@vanderbilt.edu>
Message-ID: <440DD818.9010100@stats.uwo.ca>

On 3/7/2006 12:08 PM, Charles Dupont wrote:
> Duncan Murdoch wrote:
>> I'm writing wrappers for some functions that change some of the default 
>> arguments.  I'd rather not list all of the arguments for the low level 
>> functions because there are about a dozen wrapper functions, and about 
>> 20 arguments to lowlevel.  Instead I'm trying something like this:
>> 
>> lowlevel <- function(longname = 1) {
>>    cat("longname = ", longname, "\n")
>> }
>> 
>> wrapper <- function(...) {
>>    newargs <- list(longname = 2)
>>    newargs[names(list(...))] <- list(...)
>>    do.call("lowlevel", newargs)
>> }
>> 
>> This almost works:
>> 
>>  > wrapper()
>> longname =  2
>>  > wrapper(longname = 3)
>> longname =  3
>> 
>> But it fails if I try to use partial argument matching:
>> 
>>  > wrapper(long=4)
>> Error in lowlevel(longname = 2, long = 4) :
>>          unused argument(s) (long ...)
>> 
>> because long isn't matched to longname.  Is there a reasonable way to do 
>> this (e.g. using pmatch or charmatch) other than listing all the low 
>> level arguments in the argument list to wrapper?
>> 
>> Duncan Murdoch
> 
> If all you are doing is changing the default values of some arguments
> this should work.
> 
> wrapper <- lowlevel
> formals(wrapper) <- replace(formals(lowlevel), c("longname"), list(2))

Thanks for the suggestion, but the calculation of the new defaults is 
more involved than my example indicated.  I really need to do some 
computation within the wrapper to come up with the new defaults.

Duncan Murdoch



[Rd] Expanding partial names
Duncan Murdoch