[R] eval(parse(...)) only once in a function
Thomas Lumley
tlumley at uw.edu
Mon Sep 17 22:52:38 CEST 2012
On Mon, Sep 17, 2012 at 6:27 PM, Christof Kluß <ckluss at email.uni-kiel.de> wrote:
> Hi
>
> I would like to have something like
>
> str <- "df$JT == 12"
>
> fun <- function(df) {
>
> b <- eval(parse(str))
>
> return(b)
> }
>
> but for performance "eval(parse(a))" should not be evaluated at each
> function call, but should work as
>
> fun <- function(df) {
>
> b <- df$JT == 12
>
> return(b)
> }
>
> Do you have an idea how I can implement this?
You can do it with bquote()
> e<-parse(text="df$str==12")[[1]]
> e
df$str == 12
> bquote(function(df) b<-.(e))
function(df) b <- df$str == 12
> eval(bquote(function(df) b<-.(e)))
function (df)
b <- df$str == 12
This saves more time than I expected, about 100ms per evaluation on my
computer.
-thomas
--
Thomas Lumley
Professor of Biostatistics
University of Auckland
More information about the R-help
mailing list