Unable to Append R data frame into existing Excel without overwriting

后端 未结 2 838
不知归路
不知归路 2021-01-14 13:31

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

2条回答
  •  时光说笑
    2021-01-14 14:22

    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)
    

提交回复
热议问题