Java Mailing List Archive

http://www.r-help.com/

Home » Home (12/2007) » R Help for Statistical Computing »

[R] eval(parse(text vs. get when accessing a function

Ramon Diaz-Uriarte

2007-01-05

Replies:

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@(protected)
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.
©2008 r-help.com - Jax Systems, LLC, U.S.A.