3

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.

Community
  • 1
  • 1
Bryan Hanson
  • 6,055
  • 4
  • 41
  • 78
  • `fullTitle <- as.expression(bquote(.(userTitle)~":"~Results))` – baptiste Aug 25 '13 at 02:43
  • Looking at this it does not appear that you have provided ARL or ARL[1]. `as.expression` will evaluate its arguments. – IRTFM Aug 25 '13 at 02:44
  • I'm working through this and not getting the "problem". You need to explain what you were expecting so we can explain why that _should_ not be happening. – IRTFM Aug 25 '13 at 02:55
  • @baptiste I had tried that (I think) and for me it gives the same result as Examples 3 & 4. @ Dwin I had the wrong link to the other question, just fixed it. Sorry. What I want is to have the user provide an expression which would lead to a title "Genus species: Results" where Genus and species are italicized. – Bryan Hanson Aug 25 '13 at 02:58
  • No, No, NO. You linked to a question where ARL was NOT unambiguously defined. Say what you want in natural language. If you want ARL*subscript(1) then please say something that expresses that desire. – IRTFM Aug 25 '13 at 02:58
  • Well, I admit I didn't understand how that answer would work, but I just pasted the answer `plot(1:10, main = expression(ARL[1] ~ "curve for" ~ S^2))` into the console and it worked. – Bryan Hanson Aug 25 '13 at 03:01
  • As I said. Please say what you want and then we can help. if you only complain that your code is failing, but don't express your desires in natural language, we are "dead in the water". – IRTFM Aug 25 '13 at 03:03
  • See the last part of the comment to baptiste above. Most generally, I'd like to paste a user provided expression and a fixed string together to make something that can serve as a title. – Bryan Hanson Aug 25 '13 at 03:04
  • Since we have demonstrated that we do not share a technical language, we need to understand what you meant by a "user-provided expression". An R-expression is a particular sort of language object. Is there any expectation that this "user provided expression" will conform to the requirements of an R-expression? – IRTFM Aug 25 '13 at 03:52
  • I apologize: I see why you don't understand me. I missed copying a line into the question from my minimal example. I just added it above, but the kind of user 'expression' I am talking about is `userTitle <- expression(italic(Genus)~italic(species))` Again, sorry about that. – Bryan Hanson Aug 25 '13 at 04:18

1 Answers1

3

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) )
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks so much for sticking with me. I certainly thought I must be missing something simple but I guess it really was a little tricky. In particular, in your `expression` approach, it seems really odd to have to do `.(eval(userTitle))` instead of just `.(userTitle)`. Thanks so much for figuring it out; did you find it by trial and error? It's not in the `bquote` docs and there are no examples in SO that use it (well, until now!). – Bryan Hanson Aug 25 '13 at 13:18
  • Trial and error. For me anyway the help documents are completely useless for this purpose. Hadley Wickham has some useful material in his draft book, but I agree with you that is seems really odd to need to do an eval within bquote. It seems like overkill. – IRTFM Aug 25 '13 at 21:50