[Rd] Aliasing a function

Tony Plate tplate at acm.org
Mon Feb 25 07:24:43 CET 2008


Maybe something like this? (though it seems like it might be more 
straightforward to do this sort of thing by text-processing a source 
file then source'ing it in R).

 > f <- function(a, b, c) c(a=a,b=b,c=c)
 > attr(f, "source") <- NULL
 > f
function (a, b, c)
c(a = a, b = b, c = c)
 > g1 <- f
 > ff <- formals(f)
 > argtrans <- c(a="d", b="e", c="f")
 > names(ff) <- argtrans
 > g2 <- as.function(c(ff, as.call(c(list(as.name("f")), 
lapply(argtrans, as.name)))))
 > g2
function (d, e, f)
f(a = d, b = e, c = f)
 > f(1,2,3)
a b c
1 2 3
 > g1(a=1,b=2,c=3)
a b c
1 2 3
 > g2(d=1,e=2,f=3)
a b c
1 2 3
 >

-- Tony Plate


hadley wickham wrote:
> On Sat, Feb 23, 2008 at 5:52 PM, Gabor Grothendieck
> <ggrothendieck at gmail.com> wrote:
>   
>> I assume he wants to be able to change the
>>  formals although its confusing since the example
>>  uses the same formals in both cases.
>>     
>
> Yes, that was an important point that I forgot to mention!  Thanks for
> the pointer to formals but it doesn't work in this case:
>
> function (a = 1, b = 2, c = 3)
> g(...)
>   
>> f(c=5)
>>     
> Error in f(c = 5) : '...' used in an incorrect context
>
> Hadley
>
>
>



More information about the R-devel mailing list