I wanted to answer a question regarding plotmath
but I failed to get my desired substitute
output.
My desired output:paste("Hi", paste(italic(yes),"why not?"))
and what I get: paste("Hi", "paste(italic(yes),\"why not?\")")
text<-'paste(italic(yes),"why not?")'
text
[1] "paste(italic(yes),\"why not?\")"
noqoute_text<-noquote(text)
noqoute_text
[1] paste(italic(yes),"why not?")
sub<-substitute(paste("Hi",noqoute_text),
env=list(noqoute_text=noqoute_text))
sub
paste("Hi", "paste(italic(yes),\"why not?\")")
You're using the wrong function, use
parse
instead ofnoquote
:noquote
just applies aclass
to an object of typecharacter
, with a specificprint
method not to show the quotes.In R you ought to be careful about the difference between what's in an object, and what is printed.
What
noquote
does is :"noquote"
to the class attribute of the objectThe code is :
Then when you print it, the methods
print.noquote
:"noquote"
from the object if it's thereprint
with the argumentquote = FALSE
You can actually call
print.noquote
on a string too :It does print in a similar fashion as
quote(a)
orsubstitute(a)
would but it's a totally different beast.In the code you tried, you've been substituting a string instead of a call.
For solving the question I think Moody_Mudskipperss answer works fine, but as you asked for some elaboration...
You need to be careful about different ways similar-looking things are actually stored in R, which means they behave differently.
Especially with the way
plotmath
handles labels, as they try to emulate the waycharacter
-strings are normally handled, but then applies its own rules. The 3 things you are mixing I think:character()
is the most familiar: just a string. Printing can be confusing when quotes etc. are escaped. The functionnoquote
basically tells R to mark it's argument, so that quotes are not escaped.Note that a call does not have its own evironment given with it, which means a call can give different results if evaluated e.g. from within a function.
{
Different functions can convert between these classes, but sometimes functions (such as
paste
!) also convert unexpectedly:noquote
does not do that much useful, as Moody_Mudskipper already pointed out: it only changes the printing. But the object basically remains a charactersubstitute
not only substitutes variables, but also converts its first argument into (most often) a call. Here, theprint
bites you, for when printing a call, there is no provision for special classes of its members. Try it:sub[[3]]
from the question gives[1] paste(italic(yes),"why not?")
without any backslashes! Only when printing the full call the noquote-part is lost.
parse
is used to transform a character to an expression. Nothing is evaluated yet, but some structure is introduced, so that you could manipulate the expression.paste
is often behaving annoyingly (although as documented), as it can only paste together character-strings. Therefore, if you feed it anything but a character, it firs calls as.character. So if you give it a call, you just get a text-line again. So in your question, even if you'd use parse, as soon as you start pasting thing together, you get the quotes again.Finally, your problem is harder because it's using
plotmath
s internal logic.That means that as soon as you try to evaluate your text, you'll probably get an error "could not find function italic" (or a more confusing error if there is a function
italic
defined elsewhere). When providing it in plotmath, it works because the call is only evaluated by plotmath, which will give it a nice environment, where italic works as expected.This all means you need to treat it all as an expression or call. As long as evaluation cannot be done (as long as it's you that handles the expression, instead of plotmath) it all needs to remain an expression or call. Giving substitute a call works, but you can also emulate more closely what happens in R, with