Run R function in VBA macro

*爱你&永不变心* 提交于 2019-12-03 22:00:41

You can run an R script in VBA by creating Windows Shell obejct and passing it a string that executes an R script

Sub RunRscript()
'runs an external R code through Shell
'The location of the RScript is 'C:\R_code'
'The script name is 'hello.R'

Dim shell As Object
Set shell = VBA.CreateObject("WScript.Shell")
Dim waitTillComplete As Boolean: waitTillComplete = True
Dim style As Integer: style = 1
Dim errorCode As Integer
Dim path As String
path = "RScript C:\R_code\hello.R"
errorCode = shell.Run(path, style, waitTillComplete)
End Sub

(source)

You can pass in your cell values as command line args to the R script. See ?commandArgs for more.

I am not sure this is a direct answer. You might want to look at how it is done using the Bert toolkit. This seems to help a lot in integrating R and Excel with each other.

https://bert-toolkit.com/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!