2016-12-28 57 views
0

我有一个编辑R脚本的宏。 是R脚本然后应该由下面的VBA被调用:在VBA中运行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:\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工作簿是在文件夹中

谁能帮助我出去找我的问题?

+0

,如果你尝试运行** [R脚本会发生什么**通过更换用'shell.Run路径,1,TRUE;最后一行?它工作吗? –

+0

不幸的是,不 - 我得到同样的错误。 – reggie86

+0

这个文件存放在哪里R RAM Cluster Script.R? – cyboashu

回答

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 
+0

将批处理参数的r.exe更改为rscript.exe。 – PhilC

+0

尝试在参数中添加R文件'c:\ YOUR DIR \ R RAM ...'的路径。该错误是由于rscript未找到脚本。 – PhilC

+0

再试一次? c:/ mypath备注在斜线方向上更改 – PhilC

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'}" 
+0

这确实有助于解决目录问题,但由于“程序”和“文件”之间的空间,它正在寻找名为'C :\ Program' – reggie86

+0

啊,我会修复我的答案,然后 – KySoto

+0

现在我收到PowerShell的意外令牌错误。 它说: – reggie86