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

Liaw, Andy andy_liaw at merck.com
Wed Sep 29 14:13:22 CEST 2004


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
> 
>




More information about the R-help mailing list