john.gavin@ubs.com
2004-Sep-29 11:17 UTC
[R] defining a template for functions via do.call and substitute.
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}}
Berton Gunter
2004-Sep-29 16:55 UTC
[R] defining a template for functions via do.call and substitute.
John: Andy and Dimitris have already fully answered your query. However, as you seem to mainly be interested in simple string substitution, I wanted to point out that there is a perhaps more transparent approach using gsub() that does just this. Here's a slightly more complicated function form to illustrate the idea:> f<-function(a,b){z<-a+b; b+z*sin(b)}> ## Get the body of the function and convert it to a character vector: > bf<-deparse(body(f))> orig<-c('a','b') ## original symbols > changed<-c('x','y') ## the character strings you wish to substitute forthem> ## perform the string substitution > for(i in seq(along=changed))bf<-gsub(orig[i],changed[i],bf)> ## re-parse the body and change the formals > body(f)<-parse(text=bf) > names(formals(f))<-changed> ffunction (x, y) { z <- x + y y + z * sin(y) } substitute's real power lies in its ability to substitute whole expressions bound to a symbol in an environment, and this might be overkill here. Cheers, -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > john.gavin at ubs.com > Sent: Wednesday, September 29, 2004 4:17 AM > To: r-help at stat.math.ethz.ch > Subject: [R] defining a template for functions via do.call > and substitute. > > 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 >
john.gavin@ubs.com
2004-Sep-30 10:43 UTC
[R] defining a template for functions via do.call and substitute.
Hi, Many thanks for the responses which solved my problem. Fyi, my actual application is copulaClayton <- function(u, v, alpha) (u^-alpha + v^-alpha - 1)^(-1/alpha) g <- function(){} body(g) <- do.call("substitute", list(body(copulaClayton), list(u = as.name("w"), v = quote(pnorm(var - qnorm(w)))))) formals(g) <- alist(w=, var=, alpha=) g and the substitutions seem to be working fine. Thanks to Liaw, Andy [andy_liaw at merck.com] Dimitris Rizopoulos [dimitris.rizopoulos at med.kuleuven.ac.be] Bert Gunter [gunter.berton at gene.com] and Thomas Lumley [tlumley at u.washington.edu], Gabor Grothendieck <ggrothendieck at myway.com> Regards, John. -----Original Message----- From: Gavin, John Sent: 29 September 2004 12:17 To: 'r-help at stat.math.ethz.ch' Subject: defining a template for functions via do.call and substitute. 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}}
Reasonably Related Threads
- defining a template for functions via do.call and substit ute.
- lapply with argument "X"
- Rconsole - setting the size and location of Windows help files (Rgui)
- lattice xyplot - aligning date labels so that they align with the grid lines in panel.grid
- calling R from within Java, using jri