R:在不阅读整个工作簿的情况下将工作表追加到excel工作簿

编程入门 行业动态 更新时间:2024-10-27 16:23:59
本文介绍了R:在不阅读整个工作簿的情况下将工作表追加到excel工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个26 mb的excel工作簿,我正在尝试向其中添加42 kb的工作表.使用openxlsx包,我有以下代码:

I have a 26 mb excel workbook to which I am trying to add a 42 kb worksheet. Using the openxlsx package, I have the following code:

wb_object <- loadWorkbook(to_name2) addWorksheet(wb_object, "New Data") writeData(wb_object, sheet = "New Data", m_data) saveWorkbook(wb_object, to_name2, overwrite = TRUE)

我注意到的是,这段代码需要大约2分钟的时间来执行.我相信R会读取整个26 mb的文件,然后附加42 kb的工作表.有什么方法可以将42 kb工作表附加到26 mb工作簿中,而不必读取26 mb文件?每次运行将节省2分钟.

What I have noticed is that this code takes about 2 minutes to execute. I believe R is reading in the entire 26 mb file and then appending the 42 kb worksheet. Is there any way to append the 42 kb worksheet to the 26 mb workbook without having to read in the 26 mb file? Would save 2 minutes per run.

推荐答案

我通常使用 openxlsx ,但是我不确定 openxlsx 是否可以添加无需先将Excel工作簿加载到R中即可将工作表转换为Excel文件.但是,使用 xlsx 包,您可以 添加新工作表而无需加载Excel文件.例如,如果您的文件是"test.xlsx",则可以执行以下操作:

I generally use openxlsx, but I'm not sure if openxlsx has a way to add a worksheet to an Excel file without first loading the Excel workbook into R. However, with the xlsx package, you can add a new worksheet without loading the Excel file. For example, if your file is "test.xlsx", then you could do:

library(xlsx) write.xlsx(new_data, "test.xlsx", sheetName="New_Sheet", append=TRUE)

如果我需要将任何内容保存在Excel文件中,通常我会尝试在R中执行所有操作,然后将需要写入的所有内容写到Excel文件的末尾.但是,如果您需要添加到现有的Excel文件中,则上面的代码提供了执行此操作的选项.

If I need to save anything in an Excel file, I generally try to do everything in R and then write whatever needs to go into the Excel file at the end. However, if you need to add to an existing Excel file, the above code provides an option to do that.

更多推荐

R:在不阅读整个工作簿的情况下将工作表追加到excel工作簿

本文发布于:2023-11-23 09:16:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620908.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   情况下   excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!