2017-05-31 71 views
0

我正在使用一些代码, Run R function in VBA macro在VBA中使用Shell命令在E​​xcel中运行R脚本

Sub RunRscript() 
'runs an external R code through Shell 
'The location of the RScript is 'C:/Users/abhishek.b.jai/Downloads/data' 
'The script name is 'testcode.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 = "C:/Users/abhishek.b.jai/Downloads/data/testcode.r" 
errorCode = shell.Run(path, style, waitTillComplete) 
End Sub 

我还使用相同的代码本身,但得到的错误:运行时错误429提“ActiveX组件不能创建对象”。 请帮我解决这个问题。 谢谢!

+0

你在Mac上运行吗? –

+0

检查您的参考? – Lowpar

+0

检查此答案:https://stackoverflow.com/questions/18306362/run-r-script-from-command-line –

回答

0

你应该看看这个。

http://rcom.univie.ac.at/download.html

另外,还要考虑做这种方式。

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 

仔细查看路径...

https://www.howtogeek.com/181774/why-windows-uses-backslashes-and-everything-else-uses-forward-slashes/