On Mon, Sep 17, 2012 at 6:27 PM, Christof Kluß <ckl...@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

______________________________________________
R-help@r-project.org 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.

Reply via email to