I am a beginner and am trying to simply insert an R data frame from RStudio into an existing Excel sheet without losing previous data in that sheet or overwriting the whole
What I would suggest is importing the Excel file into R Studio, create a new data frame by merging the R data frame and Excel data frame, and then exporting the file.
Something like this:
library(openxlsx)
library(dplyr)
# Get existing data and append new data
dat = readWorkbook("Reporting.xlsx", sheet="August")
dat = bind_rows(dat, new_data_frame)
# Write updated data frame to existing worksheet
wb = loadWorkbook("Reporting.xlsx")
writeData(wb, "August", dat)
# Save file (with new name for testing purposes)
saveWorkbook(wb, "Reporting_test.xlsx")
# To overwrite the pre-existing data file, you can do the following (commented out for safety)
#saveWorkbook(wb, "Reporting.xlsx", overwrite=TRUE)