简体   繁体   English

openxlsx将R数据帧通过工作表中的单元格引用写入XLSX

[英]openxlsx write R dataframe to XLSX with cell reference from worksheet to another

I am trying to write formulas from an R dataframe to XLSX file. 我正在尝试将公式从R数据帧写入XLSX文件。 I want to reference data from one worksheet in another using cell references. 我想使用单元格引用来引用另一个工作表中的数据。 One worksheet is suppposed to be a "database" and then the other sheet is supposed to have the "formula" which references data from the "database" sheet. 假设一个工作表是一个“数据库”,然后另一个工作表应该具有“公式”,该“公式”引用“数据库”表中的数据。

The R code: R代码:

require(openxlsx)

# create workbook ---------------------------------------------------------

wb <- createWorkbook()

# add worksheets ----------------------------------------------------------

addWorksheet(wb, "database")
addWorksheet(wb, "database space")
addWorksheet(wb, "reference")

# make database -----------------------------------------------------------

df1 <- data.frame(
    a = 1,
    b = 2,
    formula_1 = "A2 + B2"
)

## change class to formula
class(df1$formula_1) <- c(class(df1$formula_1), "formula")

# prepare formula ---------------------------------------------------------

df2 <- data.frame(
    formula_2 = "$database.A2",
    formula_3 = "$'database space'.B2"
)

# change class to formula
class(df2$formula_2) <- c(class(df2$formula_2), "formula")
class(df2$formula_3) <- c(class(df2$formula_3), "formula")

# write to XLSX file ------------------------------------------------------

writeData(wb, sheet = "database", x = df1)
writeData(wb, sheet = "database space", x = df1)
writeData(wb, sheet = "reference", x = df2)

# open XLSX file ----------------------------------------------------------

openXL(wb)

The usual way of adding a formula works. 添加公式的通常方法是可行的。 In the openend XLSX file this is demonstrated in cell C3 of worksheet database and database space . 在openend XLSX文件中,这在工作表database单元格C3和database space得到了证明。 But, when opening worksheet formula , my issue presents itself (see screenshot 1). 但是,当打开工作表formula ,我的问题出现了(请参见屏幕截图1)。 Cells A2 and B2 both show #NAME? 单元格A2和B2都显示#NAME? and not the value of the other worksheets. 而不是其他工作表的值。

屏幕截图1

However, when I add the cell reference manually, it works (see screenshot 2). 但是,当我手动添加单元格引用时,它可以工作(请参见屏幕截图2)。

屏幕截图2

openxlsx version openxlsx版本

> packageVersion("openxlsx")
[1] ‘4.0.17’

How can I solve this problem? 我怎么解决这个问题?

The problem here are the names that are passed to df2 . 这里的问题是传递给df2的名称。 The solution to the problem is to remove the $ and replace the . 解决该问题的方法是删除$并替换$ . with a ! ! .

Replacing df2 in the code above with the following works: 用以下工作替换上面代码中的df2

df2 <- data.frame(
    formula_2 = "database!A2",
    formula_3 = "'database space'!B2"
)

The character strings are now evaluated as formulas and correctly reference cells from other sheets. 现在将字符串作为公式求值,并正确引用其他工作表中的单元格。 I found the solution by working through the XLConnect R package, where an example in the help file for setCellFormula-methods includes a formula as a reference to a separate sheet. 我通过研究XLConnect R包找到了解决方案,其中setCellFormula-methods的帮助文件中的示例包括一个公式作为对单独工作表的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM