[R] eval(), expression()

Thomas Lumley thomas at biostat.washington.edu
Tue Jul 6 17:40:21 CEST 1999


On Tue, 6 Jul 1999, Wolfgang Koller wrote:

> Dear R-Users!
> 
> My aim is to produce boxplots without the outliers included in the 
> plot. I started to write a function that looks something like:
> 
> myboxplot <-function(x,fa) {
>   bpdata <- boxplot(x~fa,plot=FALSE)
>   bpnames <- names(bpdata)
>   for (JJ in bpnames) { 
>     command <- paste("bpdata$",JJ,"$out <- numeric(0)",sep="");
>     eval(command)
>   }
>   bxp(bpdata)
> }
> 
> Obviously this does not work as I intended since the argument 
> of eval() should be of type expression. However, with 
> expression() there is no way to have variable JJ evaluated first.


There are various possibilities. One is just to 
write
   for(JJ in names(bpdata))
	bpdata[[JJ]]$out<-numeric(0)

without all that parsing and deparsing.

In a situation where this doesn't work you can use substitute()

for(nn in names(bpdata){
command<-substitute(expression(bpdata$JJ$out<-numeric(0)),list(JJ="1"))
eval(eval(command))
}

This sort of thing is sometimes useful for annotating plots.


Thomas Lumley
Assistant Professor, Biostatistics
University of Washington, Seattle


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list