[R] eval(parse(text vs. get when accessing a function
Ramon Diaz-Uriarte
rdiaz02 at gmail.com
Sat Jan 6 14:29:59 CET 2007
On 1/5/07, jim holtman <jholtman at gmail.com> wrote:
> The other reason for considering which of the different approaches to use
> would be performance:
>
> > f.1 <- function(x) x+1
> > f.2 <- function(x) x+2
> >
> > system.time({
> + for (i in 1:100000){
> + eval(parse(text=paste('f.', i%%2+1, sep='')))(i)
> + }
> + })
> [1] 6.96 0.00 8.32 NA NA
> >
> > system.time({
> + for (i in 1:100000){
> + {if(i %% 2 == 0) f.1 else f.2}(i)
> + }
> + })
> [1] 0.52 0.00 0.61 NA NA
> >
> >
>
> eval(parse...) seems to be an order of magnitude slower. It would make a
> difference if you were calling it several thousand times; so it depends on
> your application.
Yes, that is true, thanks. Note, though, that in my case I am more
likely to do the "eval(parse(" and pasting only once, and then call
the new function thousands of times; something more like your second
version than the first.
g <- function(x, fpost) {
calledf <- eval(parse(text = paste("f.", fpost, sep = "")))
calledf(x)
## the thousands of calls to calledf go here
}
R.
>
>
> On 1/5/07, Ramon Diaz-Uriarte <rdiaz at cnio.es> wrote:
> >
> > 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: r-help-bounces at stat.math.ethz.ch
> > > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of
> Ramon Diaz-Uriarte
> > > Sent: Friday, January 05, 2007 10:02 AM
> > > To: r-help; rdiaz02 at gmail.com
> > > 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 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
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
--
Ramon Diaz-Uriarte
Statistical Computing Team
Structural Biology and Biocomputing Programme
Spanish National Cancer Centre (CNIO)
http://ligarto.org/rdiaz
More information about the R-help
mailing list