On Friday 05 January 2007 19:35, Bert Gunter wrote: > ?? > > Or to add to what Peter Dalgaard said... (perhaps for the case of many more > functions) > > Why eval(parse())? What's wrong with if then? > > g <- function(fpost,x){if(fpost==1)f.1 else f.2 }(x) > > or switch() if you have more than 2 possible arguments? I think your > remarks reinforce the wisdom of Thomas's "axiom" .
Thanks, Bert, but as with Peter's solution, your solution forces me to build g ahead of time. And again, I am not sure I see why the attempt to avoid eval(parse(text. Best, R. > > Bert Gunter > Genentech Nonclinical Statistics > South San Francisco, CA 94404 > > > -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Ramon Diaz-Uriarte > Sent: Friday, January 05, 2007 10:02 AM > To: r-help; [EMAIL PROTECTED] > Subject: [R] eval(parse(text vs. get when accessing a function > > Dear All, > > I've read Thomas Lumley's fortune "If the answer is parse() you should > usually > rethink the question.". But I am not sure it that also applies (and why) to > other situations (Lumley's comment > http://tolstoy.newcastle.edu.au/R/help/05/02/12204.html > was in reply to accessing a list). > > Suppose I have similarly called functions, except for a postfix. E.g. > > f.1 <- function(x) {x + 1} > f.2 <- function(x) {x + 2} > > And sometimes I want to call f.1 and some other times f.2 inside another > function. I can either do: > > g <- function(x, fpost) { > calledf <- eval(parse(text = paste("f.", fpost, sep = ""))) > calledf(x) > ## do more stuff > } > > > Or: > > h <- function(x, fpost) { > calledf <- get(paste("f.", fpost, sep = "")) > calledf(x) > ## do more stuff > } > > > Two questions: > 1) Why is the second better? > > 2) By changing g or h I could use "do.call" instead; why would that be > better? > Because I can handle differences in argument lists? > > > > Thanks, > > > R. -- Ramón Díaz-Uriarte Centro Nacional de Investigaciones Oncológicas (CNIO) (Spanish National Cancer Center) Melchor Fernández Almagro, 3 28029 Madrid (Spain) Fax: +-34-91-224-6972 Phone: +-34-91-224-6900 http://ligarto.org/rdiaz PGP KeyID: 0xE89B3462 (http://ligarto.org/rdiaz/0xE89B3462.asc) **NOTA DE CONFIDENCIALIDAD** Este correo electrónico, y en s...{{dropped}} ______________________________________________ R-help@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 and provide commented, minimal, self-contained, reproducible code.