Lets say I create the following Excel file:
library(openxlsx)
library(writexl)
writexl::write_xlsx(list(iris = iris, mtcars = mtcars), "example_modify_exceltab.xlsx")
Then I would like to modify an existing sheet in this file. I use the answer from the following question: How do I modify an existing a sheet in an Excel Workbook using Openxlsx package in R?
wb <- loadWorkbook("example_modify_exceltab.xlsx")
Then I add a sheet with data 'USArrests':
addWorksheet(wb, sheetName = "USArrests")
writeData(wb, sheet = "USArrests", USArrests, colNames = TRUE)
Save workbook:
saveWorkbook(wb, "example_modify_exceltab1.xlsx", overwrite = TRUE)
The problem now is that the data in file example_modify_exceltab.xlsx
has column names, and the column names in 'example_modify_exceltab1.xlsx' are empty. I would like to achieve that the sheets iris
and mtcars
still have the same column names as in example_modify_exceltab.xlsx
. How do I achieve this using openxlsx
?