openxlsx Excel formulae in a row, how to create formula dynamically for each column

雨燕双飞 提交于 2021-01-29 02:02:07

问题


My Goal

I have a data set with several columns stored in an excel file. For each column I need to insert statistical formulae on the side of the data set. The formulae should be created dynamically.

For the sake of the example, lets create an example 3 columns by 10 rows, so that anyone can follow.

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

The data set now looks like this: data set

I want to insert a set of formulae on the left of my excel data set, say column E. For instance, I need average and standard deviation for each column.

I have tried method no.1:

According to the R documentation, I can use the writeFormula method. But this will put my formulae in a column, whereas I need them in a row!! 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

I have tried method no.2

In the documentation there is the method with writeData where you have to re-class the cells as formula. If I create a data frame with dynamic formulae inside and store it to excel, perhaps I can get it working.

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

Now the data frame df look like this:

> 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)

Now, I store it to 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

It does store into excel as rows, but as explicit text and not as formula!! The result looks like this: resulting data set


回答1:


You could potentially use lapply to pass the formulas that you want to write to an Excel sheet. Note for some reason though if there is a . in an Excel formula, it doesn't seem to recognise the formula properly, so have replaced them with STDEV in this example.

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)


来源:https://stackoverflow.com/questions/62299306/openxlsx-excel-formulae-in-a-row-how-to-create-formula-dynamically-for-each-col

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!