在VBA错误目录中运行R脚本

时间:2016-12-28 17:36:54

标签: r excel windows vba excel-vba

我有一个编辑R脚本的宏。 那个R脚本应该由以下VBA调用:

 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:\Program Files\R\R-3.3.2\bin\i386\R.exe" & """ """ & "R RAM Cluster Script.R" & """"

 errorCode = shell.Run(path, style, waitTillComplete)

以上代码来自this question

但是,当我执行宏时,R命令行会给出一个错误说明:

 '\\dm\home\myaccount\*Path of my original Excel File*'
 "CMD.EXE was started with the above path as the current directory. 
  UNC Paths are not supported. Defaulting to Windows directory. 
  Argument 'R  RAM Cluster Script.R' Ignored"

脚本存储在我的Excel工作簿所在的文件夹中。

任何人都可以帮我解决问题吗?

2 个答案:

答案 0 :(得分:1)

试试这个:

Sub test()
 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:\Program Files\R\R-3.3.1\bin\RScript.exe""" & """c:/temp/R RAM Cluster Script.R"""

 errorCode = shell.Run(path, style, waitTillComplete)

End Sub

答案 1 :(得分:0)

我建议尝试使用powershell作为脚本的包装器,因为它支持UNC路径。所以,这样的事情应该有效:

path = "Powershell.exe -executionpolicy bypass -Command &{'C:\Program Files\R\R-3.3.2\bin\i386\R.exe " &  ThisWorkbook.Path & "\R RAM Cluster Script.R'}"