在Excel VBA中运行RScript时出错
问题描述:
我已经通过几种解决方案搜索了同样的问题,但我的代码仍然无法运行。确保我的文件路径中没有空格,并且仍然有三重引号可以确保。我得到运行宏的对象'IWshShell3'失败'的错误'Method'Run'。我在这里可能会错过什么?在Excel VBA中运行RScript时出错
代码:
Sub RunRscript()
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 ""G:\structureshouston\Kocian\hello.R"""
errorCode = shell.Run(path, style, waitTillComplete)
End Sub
答
我相信你的报价水平是不正确。 RScript应该在引号中,而脚本的文件名不应该是。另外,我想确保在通话中包含完整的路径名称。尝试:
path = """C:\Program Files\R\R-3.2.4revised\bin\RScript"" G:\structureshouston\Kocian\hello.R"
根据您安装的版本,您可能需要更新RScript的路径。
+0
它也帮助了我。你能否告诉我如何通过路径发送变量给R? – aman
答
它花了一点点努力去实现这一目标,但以下工作适合我。
Sub RunRscript1()
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 to R executable: C:\Users\rshuell001\Documents\R\R-3.2.5\bin\x64\R.exe
' path to R script: C:\Users\rshuell001\Documents\R\Download.r
' see more setup details here
' http://shashiasrblog.blogspot.com/2013/10/vba-front-end-for-r.html
path = "C:\Users\rshuell001\Documents\R\R-3.2.5\bin\x64\R.exe CMD BATCH --vanilla --slave C:\Users\rshuell001\Documents\R\Download.r"
'path = """C:\Users\rshuell001\Documents\R\R-3.2.5\bin\i386"" C:\Users\rshuell001\Documents\R\Download.R"
errorCode = shell.Run(path, style, waitTillComplete)
End Sub
请确保您阅读过使用下面的链接设置“权限”。
http://shashiasrblog.blogspot.com/2013/10/vba-front-end-for-r.html
你真的可以从命令行运行Rscript吗?它是否可以通过你的Path环境变量访问? – MrFlick
错误输出后是什么'errorCode'? – Jeeped
与'rscript.exe'关联的'.r'文件扩展名是什么?你可以省略程序并通过关联运行它吗? – Jeeped