从R运行VBA脚本

ddg*_*ddg 19 workflow vba r

我必须管理涉及R脚本和的脚本VBA-code.我想在R中运行该过程(我的大部分代码都是),然后调用VBA-code特定的计算.

我将在R中为VBA准备输入,在某处写入结果(.csv,数据库),然后在R脚本的其余部分中使用结果.

最好的当然是将整个代码移到R中,但现在这是不可能的.这VBA-code是相当复杂的.将其转换为R将是一项具有挑战性的长期任务.

是否有可能在R中管理这样的工作流程?

Ric*_*ton 13

  1. 编写一个调用VBA的VBscript包装器.请参阅从命令行或批处理文件运行Excel宏的方法?

  2. 通过R systemshell函数运行VBscript .

  • 可能很方便:`shell(shQuote(normalizePath(path_to_vbs_file)),"cscript",flag ="// nologo")`. (3认同)

leb*_*noz 11

这是一个不需要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()
Run Code Online (Sandbox Code Playgroud)

  • 信用到期的信用:[这里是相关问题](http://stackoverflow.com/questions/43095645/how-to-export-an-excel-sheet-range-to-a-picture-from-within- r) 这启发了我的回答。 (2认同)