Question

I am trying to paste together user a user provided string and a fixed string in a plot title.

A simple case that works, of course:

userTitle <- "user title" # Example 1
fullTitle <- paste(userTitle, ": Results", sep = "")
plot(1:10, main = fullTitle)

But what if the user's title contains an expression? Here's some things I've tried:

# This works, but doesn't include the fixed string # Example 2
userTitle <- expression(italic(Genus)~italic(species)) # EDIT: this was missing
fullTitle <- bquote(.(userTitle))
plot(1:10, main = fullTitle)

Try to add the fixed string. Some things that don't quite work:

fullTitle <- bquote(.(userTitle)~':'~Results) # Example 3
plot(1:10, main = fullTitle) # title missing .(userTitle)

fullTitle <- bquote(paste("Results:", .(userTitle))) # Example 4
plot(1:10, main = fullTitle) # title missing .(userTitle)

But this example, from here works just fine [EDIT: link was to wrong question].

x<- 232323
plot(1:10, main = bquote(paste(ARL[1], " curve for ", S^2, "; x=",.(x))))

My Example 4 looks pretty much exactly this last one, but doesn't behave the same. There are so many combos of bquote, expression, substitute and I've looked at a lot of answers, but I am likely missing something really small. Any advice on how to get the user string and fixed string together if the user string contains an expression in this case? Thanks.

Was it helpful?

Solution

I can do it with a formula:

userTitle <- italic(Genus)~italic(species) 
plot(1, 1., main=substitute( userTitle*": Results" , 
                              list(userTitle=userTitle) ) )

And now with an expression:

userTitle <- expression( italic(Genus)~italic(species) )
plot( 1, 1, main= bquote(.(eval(userTitle))*":"~Results) )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top