在没有 RExcel 的情况下从 Excel VBA 运行 R

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19170237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:38:51  来源:igfitidea点击:

Running R from Excel VBA without RExcel

rexcelvbaexcel-vba

提问by Bob Hopez

Can this process be simplified?

这个过程可以简化吗?

First, I manually open this file in R:C:\R\ExampleModel\ModelScript.R

首先,我在 R 中手动打开这个文件:C:\R\ExampleModel\ModelScript.R

From R-Editor, when the code below is run from the open ModelScript.R file, it processes the Model.R script correctly.

在 R-Editor 中,当从打开的 ModelScript.R 文件运行以下代码时,它会正确处理 Model.R 脚本。

source("C:\R\ExampleModel\Model.R", echo=T)

Within Excel, I want to run the source code above without manually opening ModelScript.R from R first. Is there anything I can modify in the VBA code below to process the source() command automatically from Excel/VBA? If a batch process is the only option, short of Rexcel, please use the example extensions provided.

在 Excel 中,我想运行上面的源代码,而无需先从 R 手动打开 ModelScript.R。我可以在下面的 VBA 代码中修改任何内容以从 Excel/VBA 自动处理 source() 命令吗?如果批处理是唯一的选择,缺少 Rexcel,请使用提供的示例扩展。

Excel 2007 VBA code:

Excel 2007 VBA 代码:

Sub RRUN()

    Dim rCommand As String
    rCommand = "C:\Program Files\R\R-3.0.0\bin\Rscript.exe --verbose C:\R\ExampleModel\ModelScript.R"

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

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

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

End Sub

Note: I tried using R.exe in place of Rscript.exe above, with no results.

注意:我尝试使用 R.exe 代替上面的 Rscript.exe,但没有结果。

回答by bill_080

You might want to look into Visual Studio Tools for Office. For Excel 2007, the VSTO link is here.

您可能需要查看Visual Studio Tools for Office。对于 Excel 2007,VSTO 链接在此处

Here's an exampleof R within Excel (written using VTSO code). Look at the section titled "Other Applications". Here's another examplefrom the same people. I have no idea if they sell those applications, or if everything is custom made.

这是Excel 中的 R示例(使用 VTSO 代码编写)。查看标题为“其他应用程序”的部分。这是同一个人的另一个例子。我不知道他们是否出售这些应用程序,或者一切都是定制的。

Here's another possible solution. I haven't pursued this method because my application has very specific requirements.

这是另一种可能的解决方案。我没有采用这种方法,因为我的应用程序有非常具体的要求。

My point is, even though you can put something together using VBA, it looks like there are better schemes.

我的观点是,即使您可以使用 VBA 将某些内容组合在一起,但似乎还有更好的方案。

回答by Charles Brewer

The following appears to work:

以下似乎有效:

Make sure that rscript.exe is in your system PATH

确保 rscript.exe 在您的系统路径中

Make sure that your inverted commas in the path= statement work for the filenames. I have left it exactly as the one I now have running rather than make it generic (and more confusing).

确保 path= 语句中的引号对文件名有效。我把它保留为我现在运行的那个,而不是让它变得通用(而且更令人困惑)。

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 ""C:\Users\Charles\SkyDrive\Documents\Development\G4H\Technical evaluation templates\Graphical analysis.R"""
errorCode = shell.Run(path, style, waitTillComplete)

End Sub

This was put together using code from http://shashiasrblog.blogspot.co.uk/2013/10/vba-front-end-for-r.html, and with advice from MrFlick. From what I can see it should also be pretty generic.

这是使用来自http://shashiasrblog.blogspot.co.uk/2013/10/vba-front-end-for-r.html 的代码并根据 MrFlick 的建议组合在一起的。从我所见,它也应该非常通用。