I have an Excel spreadsheet with 8 worksheets. Each worksheet has a different data and I have a R
code to update them.
My question is: Is there a way to export a .xlsx
file in R
to update only one specific worksheet in my Excel file without replacing the entire spreadsheet for a new file?
In my previous attempts, with the openxlsx
package, I just created a whole new file, which doesn't solve my problem.
You can solve this problem using the code below.
library(migrittr)
library(openxlsx)
file_path = '/home/user/Downloads/my_sheet.xlsx'
df <- data.frame()
file_path %>%
loadWorkbook() %T>%
removeWorksheet(sheet = "Sheet1") %T>%
addWorksheet(sheetName = "Sheet1") %T>%
writeData(sheet = "Sheet1", x = df) %T>%
saveWorkbook(., file = '/home/user/Downloads/my_sheet.xlsx', overwrite = T)