我必须管理涉及R脚本和的工作流程VBA-code。我想在R(我的大部分代码都在其中)中运行该过程,然后不时地调用VBA-code特定的计算。我会为R中的VBA准备输入,将结果写在某个地方(.csv,数据库),然后在R脚本的其余部分中使用结果。最好当然是将整个代码移到R中,但目前尚不可能。该VBA-code是相当复杂的。将其转换为R将是一项艰巨的长期任务。在R中是否有可能管理这样的工作流程?
3 回答

慕容森
TA贡献1853条经验 获得超18个赞
编写一个调用您的VBA的VBscript包装器。请参阅从命令行或批处理文件运行Excel宏的方法?
通过R system或shell函数运行VBscript

缥缈止盈
TA贡献2041条经验 获得超4个赞
这是不需要VBscript包装器的方法。您需要安装该RDCOMClient软件包
library(RDCOMClient)
# Open a specific workbook in Excel:
xlApp <- COMCreate("Excel.Application")
xlWbk <- xlApp$Workbooks()$Open("C:\\Temp\\macro_template.xlsm")
# this line of code might be necessary if you want to see your spreadsheet:
xlApp[['Visible']] <- TRUE
# Run the macro called "MyMacro":
xlApp$Run("MyMacro")
# Close the workbook and quit the app:
xlWbk$Close(FALSE)
xlApp$Quit()
# Release resources:
rm(xlWbk, xlApp)
gc()
- 3 回答
- 0 关注
- 617 浏览
添加回答
举报
0/150
提交
取消