Sebastien Bihorel
2009-Apr-29 18:30 UTC
[R] Evaluation of an expression as function argument
Dear R-users, I would like to know if is it possible to set a function argument as an evaluated expression. I have tried several syntaxes, including the following example, but could not get it anything to run. The plot function is used here but I would like to later apply the same approach to other functions. ########################## items <- c(expression(col=2),expression(pch=2)) for (in in seq(2)) { plot(1:10, eval(items[i])) } ########################## Thanks in advance for your input. Sebastien
baptiste auguie
2009-Apr-29 20:41 UTC
[R] Evaluation of an expression as function argument
Try this,> items <- c(list(col=2),list(pch=2)) > > par(mfrow=c(2, 1)) > for (ii in seq(2)) { > do.call(function(x, y, ... ) plot(1:10, ...), items[ii]) > }?do.call HTH, baptiste On 29 Apr 2009, at 19:30, Sebastien Bihorel wrote:> Dear R-users, > > I would like to know if is it possible to set a function argument as > an > evaluated expression. I have tried several syntaxes, including the > following example, but could not get it anything to run. The plot > function is used here but I would like to later apply the same > approach > to other functions. > > ########################## > items <- c(expression(col=2),expression(pch=2)) > > for (in in seq(2)) { > plot(1:10, eval(items[i])) > } > ########################## > > Thanks in advance for your input. > > Sebastien > > ______________________________________________ > 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._____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag
Sebastien Bihorel wrote:> Dear R-users, > > I would like to know if is it possible to set a function argument as an > evaluated expression. I have tried several syntaxes, including the > following example, but could not get it anything to run. The plot > function is used here but I would like to later apply the same approach > to other functions. > > ########################## > items <- c(expression(col=2),expression(pch=2)) > > for (in in seq(2)) { > plot(1:10, eval(items[i])) > } > ##########################Way 1: for(i in seq(2)) do.call("plot", c(list(1:10), as.list(items[i]))) Way 2 is perhaps easier for you: items <- list(col=2, pch=2) for (i in seq(2)) do.call("plot", c(list(1:10), items[i])) Ways 3...n up to others... Uwe Ligges> > Thanks in advance for your input. > > Sebastien > > ______________________________________________ > 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.
Sebastien Bihorel
2009-Apr-30 17:14 UTC
[R] Evaluation of an expression as function argument
Thanks Uwe and Baptiste *Sebastien Bihorel, PharmD, PhD* PKPD Scientist Cognigen Corp Email: sebastien.bihorel at cognigencorp.com <mailto:sebastien.bihorel at cognigencorp.com> Phone: (716) 633-3463 ext. 323 Uwe Ligges wrote:> > > Sebastien Bihorel wrote: >> Dear R-users, >> >> I would like to know if is it possible to set a function argument as >> an evaluated expression. I have tried several syntaxes, including the >> following example, but could not get it anything to run. The plot >> function is used here but I would like to later apply the same >> approach to other functions. >> >> ########################## >> items <- c(expression(col=2),expression(pch=2)) >> >> for (in in seq(2)) { >> plot(1:10, eval(items[i])) >> } >> ########################## > > Way 1: > > for(i in seq(2)) > do.call("plot", c(list(1:10), as.list(items[i]))) > > > Way 2 is perhaps easier for you: > > > items <- list(col=2, pch=2) > for (i in seq(2)) > do.call("plot", c(list(1:10), items[i])) > > > Ways 3...n up to others... > > > Uwe Ligges > > > > >> >> Thanks in advance for your input. >> >> Sebastien >> >> ______________________________________________ >> 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.