【问题标题】:openxlsx Excel formulae in a row, how to create formula dynamically for each column一行中的openxlsx Excel公式,如何为每列动态创建公式
【发布时间】:2020-06-10 08:40:37
【问题描述】:

我的目标

我有一个数据集,其中几列存储在一个 Excel 文件中。对于每一列,我需要在数据集的一侧插入统计公式。公式应该是动态创建的。

为了示例,让我们创建一个 3 列 x 10 行的示例,以便任何人都可以关注。

wb <- createWorkbook(title = "simulation")
addWorksheet(wb, "stats") # create a sheet
writeData(wb, "stats", data.frame(A=c(1:10)*pi, B=1/c(6:15), C=sqrt(11:20))) # store the example data frame
saveWorkbook(wb, "formula_example.xlsx", overwrite = TRUE) # save the file

数据集现在看起来像这样: data set

我想在我的 excel 数据集的左侧插入一组公式,比如 E 列。 例如,我需要每列的平均值和标准差。

我试过方法一:

根据 R 文档,我可以使用 writeFormula 方法。但这会将我的公式放在一列中,而我需要它们在一行中! https://www.rdocumentation.org/packages/openxlsx/versions/4.1.5/topics/writeFormula

wb <- loadWorkbook(xlsxFile = "formula_example.xlsx") # load the file
v1 <- c("AVERAGE(A2:A11)", "AVERAGE(B2:B11)", "AVERAGE(C2:C11)") # the vector of formulae
writeFormula(wb, sheet = "stats", x = v1, startCol = "E", startRow = 2) # column E and row 2
saveWorkbook(wb, "formula_example.xlsx", overwrite = TRUE) # save the file

我试过方法二

在文档中有 writeData 方法,您必须将单元格重新分类为公式。如果我创建一个内部包含动态公式的数据框并将其存储到 excel 中,也许我可以让它工作。

df <- data.frame() # initialize as empty data frame
df <- rbind(
  df, # append the formula rows below and create the column names with the method int2col
  sapply(1:3, function(i){paste0("AVERAGE(",int2col(i),"2:",int2col(i),"11)")})
)

df <- rbind(
  df, # do the same for standard deviation
  sapply(1:5, function(i){paste0("STDEV.S(",int2col(i),"2:",int2col(i),"11)")})
)
colnames(df) <- c("A","B","C") # set sensible names

现在数据框 df 如下所示:

> df
                A               B               C
1 AVERAGE(A2:A11) AVERAGE(B2:B11) AVERAGE(C2:C11)
2 STDEV.S(A2:A11) STDEV.S(B2:B11) STDEV.S(C2:C11)

现在,我将其存储到 excel 中:

class(df[1,]) <- c(class(df[1,]), "formula") # reclass as formula (not sure that this one is correct!!)
class(df[2,]) <- c(class(df[2,]), "formula") # in fact, it doesn't seem to work

writeData(wb, sheet = "stats", x = df, startCol = "E", startRow = 1) # set where to put the stats
saveWorkbook(wb, "formula_example.xlsx", overwrite = TRUE) # save the file

它确实以行的形式存储到 excel 中,但以显式文本而不是公式的形式存储!!结果如下所示: resulting data set

【问题讨论】:

  • 不是需要“=”符号(在 excel 中)使其成为公式吗?
  • 根据文档,它不需要'='符号,实际上方法 1 不需要,但它将一系列公式写在一列中

标签: r excel-formula openxlsx


【解决方案1】:

您可能会使用lapply 将要写入的公式传递到 Excel 工作表。 请注意,由于某种原因,如果 Excel 公式中有 .,它似乎无法正确识别该公式,因此在此示例中将其替换为 STDEV。

wb <- createWorkbook(title = "simulation")
addWorksheet(wb, "stats") 

# Data
writeData(wb, "stats", data.frame(A=c(1:10)*pi, B=1/c(6:15), C=sqrt(11:20)))

# Formula Vector
v1 <- c("AVERAGE(A2:A11)", "AVERAGE(B2:B11)", "AVERAGE(C2:C11)")
v2 <- c("STDEV(A2:A11)", "STDEV(B2:B11)", "STDEV(C2:C11)") 

# Columns where you want to store formulas
column1 <- c("E", "F", "G")

# write formula AVERAGE & STDEV 
lapply(1:length(column1), FUN = function(x) writeFormula(wb, "stats", x = v1[x], startCol = column1[x], startRow = 2))
lapply(1:length(column1), FUN = function(x) writeFormula(wb, "stats", x = v2[x], startCol = column1[x], startRow = 3))

saveWorkbook(wb, "formula_example83.xlsx", overwrite = TRUE)

【讨论】:

  • 谢谢,这是一种非常简洁的公式书写方式。我想我会使用这个建议的方法。希望数据框的全部意义在于我还可以包含标题名称,而这种方法并非如此。此外,编写公式向量的更动态的方式是v1 &lt;- sapply(1:length(column1), function(i){ paste0("AVERAGE(",int2col(i),"2:",int2col(i),"11)")}),即使用 openxlsx 中的函数 int2col()。
  • 我也有同样的问题,函数 STDEV.S 给出了一个名称错误,然后我通过替换为 _xlfn.STDEV.S 而不是用已弃用的 STDEV 来解决这个问题。请参阅其他帖子:stackoverflow.com/questions/29486671/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-04
  • 2017-04-13
  • 2017-10-15
相关资源
最近更新 更多