Jannis
2013-Sep-12 11:46 UTC
[R] substituting string variable in expression with its value
Hi, the following code works: plot(1,1, main=expression(paste("speed [", m * s^{-1}, "]"))) I would, however, like to be able to supply the value "speed" and m*s^{-a} by variables, e.g. do something like: a = 'speed' b = 'm*s^{-2}' plot(1,1, main=expression(paste(a, " [", b, "]"))) This, however, does not work as a and b are not treated as variable names in this case. Does anyone have a solution for this? Cheers jannis
Gerrit Eichner
2013-Sep-12 12:28 UTC
[R] substituting string variable in expression with its value
Hi, Jannis, maybe plot( 1, 1, main = bquote( paste( .(a), " [", .(b), "]"))) comes close to what you want, but I think you may even have to use the following to get a varying exponent really printed elevated: a <- "speed" b <- "m * s" cc <- -2 plot( 1, 1, main = bquote( paste( .(a), " [", .(b)^{.(cc)}, "]"))) Hth -- Gerrit> plot(1,1, main=expression(paste("speed [", m * s^{-1}, "]"))) > > > I would, however, like to be able to supply the value "speed" and m*s^{-a} by > variables, e.g. do something like: > > > a = 'speed' > b = 'm*s^{-2}' > > plot(1,1, main=expression(paste(a, " [", b, "]"))) > > This, however, does not work as a and b are not treated as variable names in > this case. Does anyone have a solution for this? > > > Cheers > jannis > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.