[R] How to concatenate expressions

baptiste auguie baptiste.auguie at googlemail.com
Fri Nov 20 19:00:37 CET 2009


Hi,

You can try this, though I hope to learn of a better way to do it,

a = c(quote(alpha),quote(beta),quote(gamma))
b = lapply(1:3, function(x) as.character(x))
c = c(quote('-10'^th),
  quote('-20'^th),
  quote('-30'^th))

testplot <- function(a,b,c) {

 text <-
   lapply(seq_along(a), function(ii) bquote(.(a[[ii]])*.(b[[ii]])*.(c[[ii]])))

 dev.new()

 plot(-5:5,-5:5,col=0)

 for (i in seq_along(a)) {
   text(x=i,
       y=i,
       labels=text[[i]])
 }
}

testplot(a,b,c)

HTH,

baptiste

2009/11/20 Sebastien Bihorel <Sebastien.Bihorel at cognigencorp.com>:
> Dear R-users,
>
> I am developing a plotting function, which receives expressions and
> character/numerical vectors as part of the many input arguments and which
> tries to concatenate them before displaying the result to the plot. I
> currently cannot find a way to make this concatenation works. I have read
> several posts in the list that solved this problem by pasting the different
> elements together before creating the expressions (like in
> http://tolstoy.newcastle.edu.au/R/help/02a/4790.html). I cannot implement
> this solution because my expressions and numerical/character vectors are
> passed to the function and not create inside the function. I would greatly
> appreciate any help with the following example code.
>
> Thank you
>
>
> testplot <- function(a,b,c) {
>
>  text <- as.expression(paste(a,b,c,sep=' '))
>
>  dev.new()
>
>  plot(-5:5,-5:5,col=0)
>
>  for (i in 1:3) {   text(x=i,
>        y=i,
>        labels=text[i])
>  }
> }
>
> a <- as.expression(c(bquote(alpha),bquote(beta),bquote(gamma)))
>
> b <- as.expression(1:3)
>
> c <- as.expression(c(bquote('-10'^th),
>                    bquote('-20'^th),
>                    bquote('-30'^th)))
>
> testplot(a,b,c)
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list