[R] using bquote to construct function

Gabor Grothendieck ggrothendieck at gmail.com
Thu Oct 2 07:42:10 CEST 2008


That may be a bug in R but I think there is another problem on top of that
as I don't think bquote descends into function bodies:

> z <- 2
> bquote(function(x) {x^.(z)})
function(x) {x^.(z)}

> bquote(function(x, y) { x^.(z) + y})
function(x, y) { x^.(z) + y}

> R.version.string # Vista
[1] "R version 2.7.2 (2008-08-25)"

Try it this way:

z <- 2
f <- function(x, y) {}
body(f) <- bquote({ x^.(z) + y })
eval(f)(2, 3)

On Thu, Oct 2, 2008 at 1:14 AM, Sundar Dorai-Raj
<sundar.dorai-raj at pdf.com> wrote:
> Hi, R-help,
>
> (sessionInfo at the end)
>
> I'm trying to construct a function using bquote and running into a strange
> error message. As an example, what I would like to do is this:
>
> z <- 2
> eval(bquote(function(x, y) { x^.(z) + y }))(2, 3)
>
> However, I get the following:
>
> Error in eval(expr, envir, enclos) :
>  invalid formal argument list for "function"
>
> However, if I change the command to following, it works:
>
> z <- 2
> eval(bquote(function(x) { x^.(z) }))(2)
> # [1] 4
>
> In other words, I remove the second argument. Is there a workaround for this
> without using eval(parse(text = ))?
>
> Thanks,
>
> --sundar
>
>> sessionInfo()
> R version 2.7.2 (2008-08-25)
> i386-pc-mingw32
>
> locale:
> LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
> States.1252;LC_MONETARY=English_United
> States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list