[R] use of bquote

David Winsemius dwinsemius at comcast.net
Fri Nov 22 18:46:05 CET 2013


On Nov 22, 2013, at 12:14 AM, peter dalgaard wrote:

> 
> On 22 Nov 2013, at 07:53 , Rolf Turner <r.turner at auckland.ac.nz> wrote:
> 
>> On 11/22/13 18:47, William Dunlap wrote:
>>>> a <- 2; b <- 3; xyplot(1:10 ~ a*(1:10), sub = c(bquote(a == .(a) ~ b==.(b))))
>>>> 
>>>> the subtitle contains three copies of the "a = 2  b = 3" phrase.
>>>> Why does it do that?  How do I tell it to give me only one copy?
>>> To avoid it don't wrap bquote() with c().  The following does what you asked for:
>>>    a <- 2; b <- 3; xyplot(1:10 ~ a*(1:10), sub = bquote(a == .(a) ~ b==.(b)))
>> 
>> Not for me it doesn't.  Without the c() wrapper, I get no subtitle at all.  Your recipe seems to
>> work with base graphics and plot() but not with lattice and xyplot().  Also the c() wrapper
>> seems to have no impact when used with plot().
>> 
>> Moreover I am mystified by the impact of the c() wrapper when used with xyplot().
>> 
>> The result returned by bquote() has class "call".  The result returned by c(bquote(...)) is a
>> list, of length 1, whose sole entry is of class "call" and is, as one might expect, equal to the
>> result returned by bquote().
>> 
>> But why should passing this length-1 list as the value for "sub" cause a triplication of the
>> subtitle?
> 
> I dunno either, but a hint at the reason would be to look at what happens with
> 
> xyplot(0 ~ 1, sub=list(quote(1+1)))
> 
> When you do computing on the language, sometimes quote()’ing is not sufficient protection against evaluation. That’s what expression objects are for. 
> 
> A solution seems to be
> 
> xyplot(0 ~ 1, sub=as.expression(
> 	bquote(pi==.(pi) ~ e==.(exp(1)))
> ))
> 

I had an email exchange with Deepayan a few years ago and he rightly said that the docs stated that the arguments to xlab, ylab, sub and main were supposed to be unevaluated _expressions_ . Neither `bquote` nor `substitute` return values as such:

> is.expression(substitute(a == A ~ b ==B, list(A=a, B=b) ))
[1] FALSE
> is.expression(bquote(a == .(a) ~ b==.(b)))
[1] FALSE

> 
--

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list