[R] defining a template for functions via do.call and substit ute.

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Wed Sep 29 14:43:26 CEST 2004


Or a slight modification

fun <- function(a, b) a+b
####
g <- function(){}
body(g) <- do.call("substitute", list(body(fun), list(a=as.name("x"), 
b=as.name("y"))))
formals(g) <- alist(x=, y=)
g

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/396887
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Liaw, Andy" <andy_liaw at merck.com>
To: <john.gavin at ubs.com>; <r-help at stat.math.ethz.ch>
Sent: Wednesday, September 29, 2004 2:13 PM
Subject: RE: [R] defining a template for functions via do.call and 
substit ute.


> Here's one not-so-straightforward way:
>
>> f <- function(a, b) a + b
>> flist <- as.list(f)
>> names(flist)[1:2] <- c("x", "y")
>> flist[[3]] <- do.call("substitute", list(body(f), 
>> list(a=as.name("x"),
> b=as.name("y"))))
>> g <- as.function(flist)
>> g
> function (x, y)
> x + y
>
> HTH,
> Andy
>
>> From: john.gavin at ubs.com
>>
>> Hi,
>>
>> Given a function
>>
>>   fun <- function(a, b) a + b
>>
>> how do I generate the function 'function(x, y) x + y'?
>>
>> Working from the help files and Bill Venables' R-news article
>> (June 2002),
>> I have tried various permutations with substitute without success.
>> e.g.
>>   do.call("substitute", list(fun, list(a = as.name("x"), b =
>> as.name("y"))))
>>
>> Regards,
>>
>> John.
>>
>> John Gavin <john.gavin at ubs.com>,
>> Quantitative Risk Models and Statistics,
>> UBS Investment Bank, 6th floor,
>> 100 Liverpool St., London EC2M 2RH, UK.
>> Phone +44 (0) 207 567 4289
>> Fax   +44 (0) 207 568 5352
>>
>> Visit our website at http://www.ubs.com
>>
>> This message contains confidential information and is
>> intend...{{dropped}}
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide!
>> http://www.R-project.org/posting-guide.html
>>
>>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list