[R] How to use expression(italic()) in a "vectorized" way (within a function)?

Gabor Grothendieck ggrothendieck at gmail.com
Fri Nov 26 16:24:57 CET 2010


On Fri, Nov 26, 2010 at 3:30 AM, Marius Hofert <m_hofert at web.de> wrote:
> Dear expeRts,
>
> I would like to use expression() for creating labels in a splom, as shown in the
> first minimal example below. Is there any way I can simplify having to write
> "expression(italic(...))" several times?
> The second example is what I tried so far, but I can't manage to get italic() to
> work. Moreover, it still seems bulky...
>
> Cheers,
>
> Marius
>
> ## minimal example (working but bulky)
> U <- matrix(runif(900),ncol=3)
> lattice::splom(~U[,1:3],varnames = c(expression(italic(U[1])),expression(italic(U[2])),expression(italic(U[3]))))
>
> ## also bulky (and does not incorporate italic())
> f <- function(x){
>    do.call(expression, lapply(1:3, function(j) substitute(x[y], list(x = as.name(x), y = 0+j))))
> }
> lattice::splom(~U[,1:3],varnames = f("U"))
>

bquote can be used to shorten it slightly:

   L <- lapply(1:3, function(i) bquote(italic(U[.(i+0)])))
   splom(~U[,1:3], varnames = as.expression(L))

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list