Folks: R 2.2.0 on Windows. I find the following somewhat puzzling:> a<-1; x<-0:1; y<-x## following works fine:> plot(x,y ,main= bquote(n[1] == .(a) ))## following produces an error:> plot(y~x ,main= bquote(n[1] == .(a) ))Error in paste(n[1] == 1, " and ", n[2] == 2) : object "n" not found ******************************* Note 1: I assume that this is due to the following documented behavior of plot.formula(): "Both the terms in the formula and the ... arguments are evaluated in data enclosed in parent.frame() if data is a list or a data frame." Nevertheless, the behavior seems inconsistent to me. Am I missing something (including the "I assume ..." comment)? Note 2: If one uses substitute() instead, it works fine: plot(y~x ,main= substitute(bquote(n[1] == a),list(a=a))) Any illumination, public or private, would be appreciated. Cheers, Bert -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box
Gabor Grothendieck
2006-Feb-09 20:03 UTC
[R] "main" parameter in plot.default vs plot.formula
I cannot explain it but I must have come across it since I noticed in various places in some of my code I have used, in terms of your example, the following: plot(y ~ x, main = as.expression(bquote(m[1] == .(a)))) plot(y ~ x, main = list(bquote(m[1] == .(a)))) both of which work as expected. On 2/9/06, Berton Gunter <gunter.berton at gene.com> wrote:> > Folks: > > R 2.2.0 on Windows. > > I find the following somewhat puzzling: > > > a<-1; x<-0:1; y<-x > > ## following works fine: > > plot(x,y ,main= bquote(n[1] == .(a) )) > > ## following produces an error: > > plot(y~x ,main= bquote(n[1] == .(a) )) > Error in paste(n[1] == 1, " and ", n[2] == 2) : object "n" not found > > ******************************* > > Note 1: I assume that this is due to the following documented behavior of > plot.formula(): > > "Both the terms in the formula and the ... arguments are evaluated in data > enclosed in parent.frame() if data is a list or a data frame." > > Nevertheless, the behavior seems inconsistent to me. Am I missing something > (including the "I assume ..." comment)? > > Note 2: If one uses substitute() instead, it works fine: > > plot(y~x ,main= substitute(bquote(n[1] == a),list(a=a))) > > > Any illumination, public or private, would be appreciated. > > Cheers, > Bert > > > -- Bert Gunter > Genentech Non-Clinical Statistics > South San Francisco, CA > > "The business of the statistician is to catalyze the scientific learning > process." - George E. P. Box > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Gabor Grothendieck
2006-Feb-10 01:52 UTC
[R] "main" parameter in plot.default vs plot.formula
On 2/9/06, Berton Gunter <gunter.berton at gene.com> wrote:> > Folks: > > R 2.2.0 on Windows. > > I find the following somewhat puzzling: > > > a<-1; x<-0:1; y<-x > > ## following works fine: > > plot(x,y ,main= bquote(n[1] == .(a) )) > > ## following produces an error: > > plot(y~x ,main= bquote(n[1] == .(a) )) > Error in paste(n[1] == 1, " and ", n[2] == 2) : object "n" not found > > ******************************* > > Note 1: I assume that this is due to the following documented behavior of > plot.formula(): > > "Both the terms in the formula and the ... arguments are evaluated in data > enclosed in parent.frame() if data is a list or a data frame." > > Nevertheless, the behavior seems inconsistent to me. Am I missing something > (including the "I assume ..." comment)? > > Note 2: If one uses substitute() instead, it works fine: > > plot(y~x ,main= substitute(bquote(n[1] == a),list(a=a))) >Regarding your note 2, the key thing that seems to be necessary is not really substitute vs. bquote but just doing it twice. In your example above you did not replace bquote with substitute but did a substitute and a bquote so now its two levels deep. But if we just did two bquotes or two substitutes that would be ok too. For example, we can just apply bquote twice and then it works: plot(y~x ,main = bquote(bquote(n[1] == .(a)))) The various calls must be stripping off one layer so that you have to protect it twice so that the underlying code does not get evaluated.