Unable to Append R data frame into existing Excel without overwriting

后端 未结 2 849
不知归路
不知归路 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:03

    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.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题