Home » excel » Superscript from R to Excel table?

Superscript from R to Excel table?

Posted by: admin April 23, 2020 Leave a comment


There are several great R packages for reading and writing MS Excel spreadsheets. Exporting superscripts from R is easy to LaTeX tables (see also this), but is there a way to directly export superscripts from R to an Excel table?

An example:

dt <- data.frame(a = 1:3, b = c("a", "b", ""))
dt$try1 <- paste0(dt$a, "^{", dt$b, "}") ## Base R, openxlsx does not seem to know how to handle expression()  
dt$try2 <- paste0(dt$a, "\textsuperscript{", dt$b, "}") # Should work in xtable
dt$try3 <- paste0("\textsuperscript{", dt$b, "}") # This does not work either

write.xlsx(dt, "Superscript test.xlsx") 

The code produces a nice Excel table, but does not process LaTeX code (understandable, as we are exporting to Excel). Maybe there is a superscript code for Excel to bypass this issue?

How to&Answers:

This question has been in here for a while and I imagine OP has found a solution. In any case, my solution is entirely based on this open git issue.

For this to work, you need to define a superscript notation and create a separate column just like what you did in dt1$try1. I enclosed the superscript characters in _[] in my example. Just try to avoid ambiguous notation that may be found in other situations in your workbook.

dt <- data.frame(a = 1:3, b = c("a", "b", ""))

dt$sup <- paste0(dt$a, "_[", dt$b, "]") # create superscript col, enclosed in '_[]'

wb <- openxlsx::createWorkbook() # create workbook

openxlsx::addWorksheet(wb, sheetName = "data") # add sheet

openxlsx::writeData(wb, sheet=1, x=dt, xy=c(1, 1)) # write data on workbook

for(i in grep("\_\[([A-z0-9\s]*)\]", wb$sharedStrings)){
  # if empty string in superscript notation, then just remove the superscript notation
  if(grepl("\_\[\]", wb$sharedStrings[[i]])){
   wb$sharedStrings[[i]] <- gsub("\_\[\]", "", wb$sharedStrings[[i]])
   next # skip to next iteration

  # insert additioanl formating in shared string
  wb$sharedStrings[[i]] <- gsub("<si>", "<si><r>", gsub("</si>", "</r></si>", wb$sharedStrings[[i]]))

  # find the "_[...]" pattern, remove brackets and udnerline and enclose the text with superscript format
  wb$sharedStrings[[i]] <- gsub("\_\[([A-z0-9\s]*)\]", "</t></r><r><rPr><vertAlign val=\"superscript\"/></rPr><t xml:space=\"preserve\">\1</t></r><r><t xml:space=\"preserve\">", wb$sharedStrings[[i]])

openxlsx::saveWorkbook(wb, file="test.xlsx", overwrite = TRUE)

wb$sharedStrings contains the unique instances of strings in your workbook cells. The pattern chosen captures any instance of word, digit and space (or empty string) enclosed in _[]. The first part of the loop checks for an absence of characters in the superscript notation and it removes the notation if TRUE.