通过VBA执行时R忽略R脚本

mor*_*org 3 excel vba r

我正在尝试从excel VBA运行R脚本。该脚本仅查询数据,格式化和组织数据,然后将其导出为单独的XLS文件。但是,我无法运行R脚本。

本文的结束语:(尽管OG代码是我所了解的) 在没有RExcel的情况下从Excel VBA运行R 我由于下载的工作限制而无法访问RExcel。我已经找到我的R.exe文件路径,并且下面的宏可以运行:

 Dim rCommand As String
rCommand = "C:\Program Files\R\R-3.5.3\bin\R.exe --verbose U:\Reporting\Mix of Business Report\Morgan's Trials\Mix of Business Report v1.R"

'Timer Set to run full Model.R script
Application.Wait Now + TimeValue("00:01:05")

'Runs R Script and Arguments into process
shell rCommand, vbNormalFocus

'Timer Set to run full Model.R Script
Application.Wait Now + TimeValue("00:01:05")
Run Code Online (Sandbox Code Playgroud)

我希望宏在后台运行,但是R窗口(只要工作正常,就不要Rstudio了),它不能运行R脚本。

这是在“ Rterm”窗口中显示的内容:

CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.

ARGUMENT 'U:\Reporting\Mix' __ ignored__

ARGUMENT 'of' __ ignored__

ARGUMENT 'Business' __ ignored__

ARGUMENT 'Report\Morgan's' __ ignored__

ARGUMENT 'Trials\Mix' __ ignored__

ARGUMENT 'of' __ ignored__

ARGUMENT 'Business' __ ignored__

ARGUMENT 'Report' __ ignored__

ARGUMENT 'v1.R' __ ignored__

'verbose' and 'quietly' are both true; being verbose then ..
now dyn.load("C:/Program Files/R/R-3.5.3/library/methods/libs/x64/methods.dll") ...

'verbose' and 'quietly' are both true; being verbose then ..
'verbose' and 'quietly' are both true; being verbose then ..
now dyn.load("C:/Program Files/R/R-3.5.3/library/utils/libs/x64/utils.dll") ...
'verbose' and 'quietly' are both true; being verbose then ..
Garbage collection 1 = 0+0+1 (level 2) ...
11.5 Mbytes of cons cells used (35%)
2.7 Mbytes of vectors used (4%)
now dyn.load("C:/Program Files/R/R-3.5.3/library/grDevices/libs/x64/grDevices.dll") ...
'verbose' and 'quietly' are both true; being verbose then ..
now dyn.load("C:/Program Files/R/R-3.5.3/library/graphics/libs/x64/graphics.dll") ...
'verbose' and 'quietly' are both true; being verbose then ..
now dyn.load("C:/Program Files/R/R-3.5.3/library/stats/libs/x64/stats.dll") ...
 ending setup_Rmainloop(): R_Interactive = 1 {main.c}
 >R_ReplConsole(): before "for(;;)" {main.c}
Run Code Online (Sandbox Code Playgroud)

为什么我的文件路径被忽略?我还尝试将冗长的r脚本路径的反斜杠更改为正斜杠,即相同的消息。

抱歉,如果是重复的话,我不理解其他任何问题,上面链接的问题是我的最佳选择。

Lou*_*uis 5

似乎问题出在路径上,因为UNC您需要在包含空格的路径中加上引号。像这样更改命令,它将起作用:

Dim rCommand As String
rCommand = """C:\Program Files\R\R-3.5.3\bin\R.exe"" --verbose ""U:\Reporting\Mix of Business Report\Morgan's Trials\Mix of Business Report v1.R"""
Run Code Online (Sandbox Code Playgroud)

编辑

另一个问题是R.exe不应仅用于执行脚本。RScript.exe为此,我们可以使用&& pause该命令,并且可以通过在命令末尾添加来避免控制台关闭。这是完整的代码:

    Dim rCommand As String
    rCommand = """C:\Program Files\R\R-3.5.3\bin\RScript.exe"" --verbose ""U:\Reporting\Mix of Business Report\Morgan's Trials\Mix of Business Report v1.R"" && pause"

    'Timer Set to run full Model.R script
    Application.Wait Now + TimeValue("00:01:05")

    'Runs R Script and Arguments into process
    Shell rCommand, vbNormalFocus

    'Timer Set to run full Model.R Script
    Application.Wait Now + TimeValue("00:01:05")
Run Code Online (Sandbox Code Playgroud)

同步方式

改进功能的一种方法是使用带有waitTillComplete标志的外壳程序执行,该外壳程序通过Sync调用来执行命令。这是代码:

Sub R_Exec()
    Dim cmd As Object
    Dim rCommand As String, rBin As String, rScript As String
    Dim errorCode As Integer
    Dim waitTillComplete As Boolean: waitTillComplete = True
    Dim debugging As Boolean: debugging = True
    Set cmd = VBA.CreateObject("WScript.Shell")

    rBin = """C:\Program Files\R\R-3.5.3\bin\RScript.exe"""
    rScript = """U:\Reporting\Mix of Business Report\Morgan's Trials\Mix of Business Report v1.R"""

    'Check if the shell has to keep CMD window or not
    If debugging Then
        rCommand = "cmd.exe ""/k"" " & rBin & " " & rScript
    Else
        rCommand = rBin & " " & rScript
    End If

    Debug.Print rCommand 'Print the command for debug

    'Runs R Script and Arguments into process, returning errorCode
    errorCode = cmd.Run(rCommand, vbNormalFocus, waitTillComplete)
End Sub
Run Code Online (Sandbox Code Playgroud)

这样,Sub您可以决定是否使外壳窗口保持打开状态。

希望这可以帮助。

  • 找到了解决方案并在我的R控制台上进行了测试!我将更新答案!;) (2认同)