无法在不覆盖的情况下将 R 数据框追加到现有 Excel 中 [英] Unable to Append R data frame into existing Excel without overwriting

查看:36
本文介绍了无法在不覆盖的情况下将 R 数据框追加到现有 Excel 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者,我试图简单地将 RStudio 中的 R 数据框插入现有的 Excel 工作表中,而不会丢失该工作表中以前的数据或覆盖整个文件,例如.从第 16 行第 1 列的August"工作表中的文件Reporting.xlsx"中插入新数据框(15 行 4 列),这样以前的信息就不会丢失?

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 file, eg. insert new data frame (15 rows, 4 columns) in file "Reporting.xlsx" in Sheet "August" from row 16, column 1, so that the previous info is not lost ?

谢谢!

推荐答案

是这样的:

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)

这篇关于无法在不覆盖的情况下将 R 数据框追加到现有 Excel 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆