上标从 R 到 Excel table?
Superscript from R to Excel table?
有 several great R packages for reading and writing MS Excel spreadsheets. Exporting superscripts from R is easy to LaTeX tables (see also this),但是有没有办法直接将上标从 R 导出到 Excel table?
一个例子:
library(openxlsx)
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")
该代码生成了一个不错的 Excel table,但不处理 LaTeX 代码(可以理解,因为我们正在导出到 Excel)。也许Excel有一个上标代码可以绕过这个问题?
这个问题在这里已经有一段时间了,我想 OP 已经找到了解决方案。无论如何,我的解决方案完全基于this open git issue。
为此,您需要定义一个上标符号并创建一个单独的列,就像您在 dt1$try1
中所做的那样。在我的示例中,我将上标字符括在 _[]
中。尽量避免在您的工作簿中的其他情况下可能会发现的含糊不清的符号。
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
包含工作簿单元格中字符串的唯一实例。所选模式捕获包含在 _[]
中的单词、数字和 space(或空字符串)的任何实例。循环的第一部分检查上标符号中是否缺少字符,如果 TRUE
.
则删除符号
有 several great R packages for reading and writing MS Excel spreadsheets. Exporting superscripts from R is easy to LaTeX tables (see also this),但是有没有办法直接将上标从 R 导出到 Excel table?
一个例子:
library(openxlsx)
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")
该代码生成了一个不错的 Excel table,但不处理 LaTeX 代码(可以理解,因为我们正在导出到 Excel)。也许Excel有一个上标代码可以绕过这个问题?
这个问题在这里已经有一段时间了,我想 OP 已经找到了解决方案。无论如何,我的解决方案完全基于this open git issue。
为此,您需要定义一个上标符号并创建一个单独的列,就像您在 dt1$try1
中所做的那样。在我的示例中,我将上标字符括在 _[]
中。尽量避免在您的工作簿中的其他情况下可能会发现的含糊不清的符号。
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
包含工作簿单元格中字符串的唯一实例。所选模式捕获包含在 _[]
中的单词、数字和 space(或空字符串)的任何实例。循环的第一部分检查上标符号中是否缺少字符,如果 TRUE
.