Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to evaluate a varible inside an expression to use as an axis label?

I am trying to evaluate a variable inside theexpression function. The function works fine when the variable has no value and is just a string.

> plot(rnorm(30), xlab = expression(value~2^-dCT))

enter image description here

But when value is a variable, the value of the variable gets ignored…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

> rm(value)
> value = "some text"
> plot(rnorm(30), xlab = expression(value~2^-dCT))

enter image description here

I also tried > plot(rnorm(30), xlab = expression(eval(value)~2^-dCT)) and had a similar issue…
enter image description here

plot(rnorm(30), xlab = expression(paste(value~2^-dCT))) does not work as well. Any help would be appreciated, thank you!

>Solution :

Here are several ways:

value <- "some text"

# 1
plot(0, xlab = substitute(value ~ 2^-dCT, list(value = value)))

# 2
plot(0, xlab = bquote(.(value) ~ 2^-dCT))

# 3
plot(0, xlab = parse(text = sprintf("'%s' ~ 2^-dCT", value)))

# 4
fo <- value ~ 2^-dCT
fo[[2]] <- as.name(value)
plot(0, xlab = fo)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading