I am trying to fill some columns based on a formula. I wrote the formula, ran the code and saved the workbook by calling saveWorkbook() function. When I open the excel to have a look everything seems fine. But when I read the data from the same sheet I am getting NAs for the columns that I filled earlier with formula.
Here is a simple example. Initially I have two columns: a and b
read.xlsx("data/rough.xlsx", sheet = "Sheet1", rows = c(1:4), cols = c(2,3))
a b
1 1 NA
2 2 NA
3 3 NA
I loaded the workbook and did the required changes
wb <- loadWorkbook("data/rough.xlsx")
formula_vector <- c(paste0("B", seq(2,4), "*2"))
writeFormula(wb, sheet = "Sheet1", x = formula_vector, startCol = 3, startRow = 2)
saveWorkbook(wb, "data/rough.xlsx", overwrite = TRUE)
After saving the workbook I ran the same line and got the same output
read.xlsx("data/rough.xlsx", sheet = "Sheet1", rows = c(1:4), cols = c(2,3))
a b
1 1 NA
2 2 NA
3 3 NA
this is what I actually have inside the sheet
Now I that I manually opened the file it is asking me whether to save or not even though I didn't change anything. If I save it and the run the same line in R...I am getting the correct values.
after manually saving the file
read.xlsx("data/rough.xlsx", sheet = "Sheet1", rows = c(1:4), cols = c(2,3))
a b
1 1 2
2 2 4
3 3 6
How can I resolve this? It seems like even after running the saveWorkbook() R is still holding some details of that workbook.
Please let me know if you come across any solutions. Thank you!