1

Here is an example:

   PMEx <- function(t) {
   y <- (1:10)^2
   plot(1:10,y,type="l")
   text(2,90,label="This is NOT what I want -->",adj=0)
   text(2,80,label="This IS what I want -->",adj=0)
   text(2,70,label="This is NOT what I want -->",adj=0)  
   #  --- These are representative lines of code --- 
   text(5,90,label=expression("x[0]"))  # This is NOT what I want 
   text(5,80,label=expression(x[0]))    # This IS what I want
   text(5,70,label=expression(t))       # This is NOT what I want  
} # End of PMEx().  
RunPMEx <- function() { 
   PMEx("x[0]") 
} # End of RunPMEx().  
RunPMEx()

This code generates a plot with text lines, one showing what I want (the letter x with subscript 0) and two others showing what happens (NOT what I want) when expression() is used. I have tried various suggestions in Stack Overflow, but none of them work in this case (e.g. using bquote(.(t)), paste, parse, substitute). The last text(...) line of PMEx(t) is a prototype for what I want.

John Coleman
  • 51,337
  • 7
  • 54
  • 119
GWB
  • 11
  • 2

1 Answers1

2

You will have to parse that string into an expression. Use

text(5,70,label=parse(text=t)) 

for example

test <- function(t) {
    plot(0:1, 0:1)
    text(.5, .5, label =parse(text=t))
}
test("x[0]")

Or you could pass in a quoted expression rather than a string

test2 <- function(q) {
    plot(0:1, 0:1)
    text(.5, .5, label=q)
}
test2(quote(x[0]))
MrFlick
  • 195,160
  • 17
  • 277
  • 295