[R] Evaluate an expresion comming from string

. . xkziloj at gmail.com
Thu Sep 1 05:47:58 CEST 2011


...and this also does not work:

bench <- function(str,...) {
  func <- function(x) {
    x+10
  }
  expr <- list(...)[1]
  str <- gsub("XXX",expr,str)
  x <- as.expression(gsub("\"","",str))
  eval(x)
}
bench("func(XXX)", "1")

Comments are appreciable.

On Thu, Sep 1, 2011 at 12:12 AM, . . <xkziloj at gmail.com> wrote:
> Whats wrong here?
>
> I was expecting 11 as the result...
>
> Thanks in advance.
>
> bench <- function(str,...) {
> func <- function(x) x+10
>  expr <- list(...)[1]
>  str <- gsub("XXX",expr,str)
>  x <- as.call(gsub("\"","",str))
>  eval(x)
> }
> bench("func(XXX)", "1")
>



More information about the R-help mailing list