【问题标题】:Superscript from R to Excel table?从R到Excel表格的上标?
【发布时间】:2016-10-25 08:15:02
【问题描述】:

several great R packages for reading and writing MS Excel spreadsheets。从R is easy to LaTeX tables 导出上标(另见this),但有没有办法将上标从R 直接导出到Excel 表?

一个例子:

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 表格,但不处理 LaTeX 代码(可以理解,因为我们正在导出到 Excel)。也许Excel有一个上标代码可以绕过这个问题?

【问题讨论】:

    标签: r excel export-to-excel


    【解决方案1】:

    这个问题在这里已经有一段时间了,我想 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 包含工作簿单元格中字符串的唯一实例。选择的模式捕获包含在_[] 中的任何单词、数字和空格(或空字符串)实例。循环的第一部分检查上标符号中是否缺少字符,如果 TRUE 则删除符号。

    【讨论】:

    • 不,我没有找到解决方案。不得不手动完成。非常感谢您花时间回答!我希望这个答案可以帮助人们解决这个问题。
    • 感谢 JdeMello,请您告诉我如何使那行 xml 代码在内部生成粗体文本,以便在单元格“C2:C3”中生成粗体文本?
    猜你喜欢
    • 2019-02-20
    • 1970-01-01
    • 2022-10-05
    • 2019-11-27
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多