简体   繁体   中英

How to generate an array of “language”-objects in R?

I want to store formatted legend texts in an array.

normFactor= c(1e-1, 1e-2, 1e-3)

legend_text[1] = bquote(mu ~ "=" ~ .(normFactor[1]))
legend_text[2] = bquote(mu ~ "=" ~ .(normFactor[2]))
legend_text[3] = bquote(mu ~ "=" ~ .(normFactor[3]))

The bquote-output seems to be of the type language :

str(bquote(mu ~ "=" ~ .(normFactor[3])))

language mu ~ "=" ~ 0.001

So I thought I could set up an array of language-Elements:

legend_text = language()

Unfortuantely this does not work, and I'm wondering how I can store those elements in a array,...

You can store them in a list, or even better create them like this:

lapply(seq_along(normFactor),
         function(i)bquote(mu ~ "=" ~ .(normFactor[i])))

[[1]]
mu ~ "=" ~ 0.1

[[2]]
mu ~ "=" ~ 0.01

[[3]]
mu ~ "=" ~ 0.001

bquote create a language object may in fact be converted to and from lists using as.list and as.call . So the solution a bove can be modified for exmaple:

> ll <- lapply(seq_along(normFactor),
+        function(i)as.list(bquote(mu ~ "=" ~ .(normFactor[i]))))
> str(ll)
List of 3
 $ :List of 3
  ..$ : symbol ~
  ..$ : language mu ~ "="
  ..$ : num 0.1
 $ :List of 3
  ..$ : symbol ~
  ..$ : language mu ~ "="
  ..$ : num 0.01
 $ :List of 3
  ..$ : symbol ~
  ..$ : language mu ~ "="
  ..$ : num 0.001

Then coercing the result to a list of calls:

> lapply(ll,as.call)
[[1]]
mu ~ "=" ~ 0.1

[[2]]
mu ~ "=" ~ 0.01

[[3]]
mu ~ "=" ~ 0.001

language is not an atomic data type, so the outcome will be a list rather than a vector. Dynamic typing is your friend here:

legend_text<- c(
 bquote(mu ~ "=" ~ .(normFactor[1])),
 bquote(mu ~ "=" ~ .(normFactor[2])),
 bquote(mu ~ "=" ~ .(normFactor[3]))
)

I don't know if this os what you actually need for your legend, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM