【问题标题】:R ignoring R script when excecuting through VBA通过VBA执行时忽略R脚本
【发布时间】:2019-09-17 13:38:19
【问题描述】:

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

离开这篇文章:(尽管我理解的是 OG 代码) Running R from Excel VBA without RExcel 由于下载工作限制,我无法访问 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")

我希望我的宏在后台运行,但是 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}

为什么我的文件路径被忽略了?我还尝试将详细 r 脚本路径的反斜杠更改为正斜杠,同样的消息。

对不起,如果它是重复的,我不明白发布的任何其他问题,上面链接的问题是我最好的选择。

【问题讨论】:

  • 看起来一个问题是文件名中的空格。确保以适合您的操作系统的方式正确转义它们,以免它们看起来像命令行上的单独参数。
  • 说得通!我该怎么做?

标签: r excel vba


【解决方案1】:

似乎问题出在路径上,因为在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"""

编辑

另一个问题是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")

同步方式

改进功能的一种方法是使用waitTillComplete 标志执行shell,它使用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

有了这个Sub,你可以决定是否保持shell窗口打开。

希望这会有所帮助。

【讨论】:

  • 是的,不,它有帮助哈哈,它现在将路径视为一项,但它仍然被忽略。 CMD.EXE 以上述路径作为当前目录启动。不支持 UNC 路径。默认为 Windows 目录。 ARGUMENT 'U:/Reporting/Mix of Business Report/Morgan's Trials/Mix of Business Report v1.R'忽略
  • 哦,我注意到R.exe 路径中也有一个空格!尝试在此处插入引号。
  • 找到解决方案并使用我的 R 控制台进行了测试!我会更新答案! ;)
  • @morg 我已经更新了代码。有了这个,您不再需要使用Application.Wait,这可能会大大减慢您的执行速度。看一下这个。 ;)
【解决方案2】:

另外,为了使代码更简洁,以避免过多的引号处理和可扩展到许多参数,请考虑从参数数组 (borrowed from @UriGoren) 构建命令行调用。另外,使用CMD.exe /k 来启动自动Rscript 的窗口(理想情况下应该可以通过PATH 环境变量访问):

Sub Run_R_Script()
    Dim args(0 To 3) As String
    Dim rCommand As String
    Dim i As Integer

    args(0) = "CMD.exe"
    args(1) = "/k"
    'args(2) = "Rscript.exe"                          ' IF R BIN FOLDER IN PATH ENV VARIABLE
    args(2) = "C:\Program Files\R\R-3.5.3\bin\RScript.exe"
    args(3) = "U:\Reporting\Mix of Business Report\Morgan's Trials\Mix of Business Report v1.R"

    rCommand = args(0)
    For i = 1 To UBound(args)
        rCommand = rCommand & " """ & args(i) & """"
    Next i        
    Debug.Print rCommand                              ' CHECK COMMAND LINE CALL

    '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")
End Sub

【讨论】:

  • 嗨!它似乎运行良好(即没有抛出错误)但是,我认为它实际上并没有运行脚本,因为在它的末尾,脚本具有在同一文件中运行不同宏的代码。当我自己获取脚本(不是由 VBA 提示)时,它可以正常运行宏,但是当我运行您帮助我设置的原始宏时,它不会
  • 请打开 Powershell 并运行从Debug.Print 输出的命令。请务必检查文件路径和/或版本。现在,我无法验证您的说法,但语法表明它应该可以工作。可能由于某种原因,R 脚本在 VBA 中运行时出错。在 R 中,请务必调用绝对路径或使用设置工作目录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
相关资源
最近更新 更多