【问题标题】:Error running RScript in Excel VBA在 Excel VBA 中运行 RScript 时出错
【发布时间】:2016-04-14 21:47:42
【问题描述】:

我已经针对同一问题搜索了多个解决方案,但我的代码仍然无法正常工作。确保我的文件路径中没有空格,并且仍然有三重引号以确保。我从运行宏中得到“对象'IWshShell3'的方法'运行'失败”错误。我会在这里遗漏什么?

代码:

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 环境变量访问吗?
  • 出错后的errorCode 是什么?
  • .r 文件扩展名是否与 rscript.exe 关联?可以省略程序,通过关联运行吗?
  • 我可以从 cmd 运行 Rscript,是的。这样做时效果很好。我实际上只是现在尝试了一下,并没有收到任何错误消息。周末或其他任何事情都没有改变。太奇怪了。

标签: r vba shell excel


【解决方案1】:

我认为您的报价水平不正确。 RScript 应该用引号引起来,而脚本的文件名不应该用引号引起来。另外,我想确保在调用中包含完整的路径名。试试:

path = """C:\Program Files\R\R-3.2.4revised\bin\RScript"" G:\structureshouston\Kocian\hello.R"

您可能需要根据已安装的版本更新 RScript 的路径。

【讨论】:

【解决方案2】:

这需要一点点努力,但以下对我有用。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2017-11-28
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多