Search code examples
rformatrtfsuperscript

How to literally print superscripts in R not used in labels or legends?


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.


Solution

  • 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)