I would like to evaluate an expression given as a string inside substitute()
, but I don't get the expected output (an x-axis label "My index"... with the subscript):
mystrng <- "index[1]" # string
plot(0~1, xlab = substitute("My"~ind, list(ind = mystrng)))
plot(0~1, xlab = substitute("My"~ind, list(ind = parse(text = mystrng))))
plot(0~1, xlab = eval(substitute("My"~ind, list(ind = parse(text = mystrng)))))
Note:
1) Without substitute()
, one can work with parse(text = mystrng)
. See also here. As this link also reveals, one should not work with strings, but, e.g., quote(index[1])
which works with the first plot()
call above. I'm just interested in how one would go about strings.
2) The last plot()
call trial was inspired by ?substitute
3) Ideally the solution would also work if mystrng
contains blanks, like "My index[1]"