1

I know that in a graph one can use expression() to include superscripts, e.g.

plot(rnorm(20), xlab = expression(paste("n"^"th")))

However, I literally want to print out superscripts, so I can store them in rtf files later. In fact, I want to use them to indicate the degree of statistical significance associated with my regression coefficients (you know, like "0.24^*").

With "expression(paste("n"^"th"))", I tried print(), cat(), eval(), and get() but none of them worked.

I am currently using the "rtf" package to output ".doc" files. If someone suggests using other ways/packages, it is OK if R itself cannot display rich formats in the console, but the results can be stored in some files anyway.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
wen
  • 1,875
  • 4
  • 26
  • 43

2 Answers2

4

As you've seen, the ?plotmath expressions only work within plots. Nothing in R is optimized for rtf output.

But reading the RTF vignette, it's clear you can write rtf commands in your output and there is a command for superscripting. Try

library(rtf)

rtf<-RTF("text.doc")
addText(rtf,"Hello\n")
addText(rtf,"0.05{\\super*}\n")
addText(rtf,"0.15*\n")
addText(rtf,"End")
done(rtf)
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • It works like a charm. Thank you so much! Did you just read it and figure it out, or did you know it all alone? – wen Mar 27 '15 at 00:47
  • 3
    I did some research and tested some ideas. It's pretty much how I answer all questions. – MrFlick Mar 27 '15 at 00:54
0
plot(rnorm(20), xlab = as.character("n^*"))

or even

plot(rnorm(20), xlab = paste0("n","^","th"))

Not sure why you want to use expression()

Michael
  • 1,537
  • 6
  • 20
  • 42
  • 1
    No, this is not what I want. First, "n^th" does not give me n_superscript_th. Second, it must not be displayed via a graph. It must be either directly displayed in the console or stored in some rtf files. – wen Mar 25 '15 at 22:39
  • `myExpression <- as.name(paste0("n","^","0"))` ?? – Michael Mar 25 '15 at 22:44