Re: [Rd] turning expression object to function

From: Antonio, Fabio Di Narzo <antonio.fabio_at_gmail.com>
Date: Tue 19 Dec 2006 - 10:16:44 GMT

Tnx all for the interesting (and all working) suggestions!

Best regards,
Antonio.

2006/12/19, Gabor Grothendieck <ggrothendieck@gmail.com>:
> Here is one possibility. It does not use the second argument in your function
> call but instead assumes the arguments of the output function are
> those variables
> in the expression that have not been assigned in the list L in the
> order encountered.
>
> library(gsubfn)
> asFun <- function(e, L = NULL, env = parent.frame()) {
> fo <- ~ x
> fo[[2]] <- e[[1]]
> fun <- fn$force(as.formula(do.call(substitute, list(fo, L))))
> environment(fun) <- env
> fun
> }
>
> asFun(expression(a+b+c), list(b = 1, c = 2)) # function (a) a + 1 + 2
>
>
>
>
> On 12/18/06, Antonio, Fabio Di Narzo <antonio.fabio@gmail.com> wrote:
> > Dear all,
> > I have the following problem.
> >
> > Given an expression object 'expr' containing a certain set of symbols
> > (say 'a', 'b', 'c'), I would like to translate the expression object
> > in an R function of, say, 'a', programmatically. Here an example of
> > what I mean.
> >
> > Given:
> > > expr <- expression(a+b+c)
> >
> > a call like:
> > > asFunctionOf(expr, 'a', list(b=1, c=2))
> >
> > should return a function (not necessarly formally) equivalent to
> > > function(a) a+1+2
> >
> > Some suggestions?
> >
> > Best regards,
> > Antonio.
> >
> > ______________________________________________
> > R-devel@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue Dec 19 21:18:52 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 19 Dec 2006 - 13:30:59 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.