r - Windows 脚本宿主失败

标签 r excel vba wsh

我正在尝试使用 VBA 从 Excel 运行 R 脚本。理想情况下,我会使用 .R (Rscript) 名称来调用 R 并运行该进程,或者如果这不起作用,则调用 Rscript.exe 并执行传递给它的文件名。

REXcel 不好,因为它需要 32 位版本的 Excel(而且我不在 1989 年工作)

我在( http://shashiasrblog.blogspot.co.uk/2013/10/vba-front-end-for-r.html )找到了一个似乎完美的脚本,经过适当的本地化后,它看起来像这样:

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

此操作失败并显示消息

运行时错误“-2147024894 (80070002)”: 对象“IWshShell3”的方法“Run”失败。

这根本没有告诉我任何事情。我尝试用谷歌搜索错误消息,但一无所获。

我已将 PATH 变量设置为包含 R 和 Rscript 所在的目录。

我怀疑这很简单,但似乎缺乏从 Excel 执行 R 的简单方法。

最佳答案

您需要将 Rscript 添加到您的路径中,否则 shell 不知道将文件发送到哪个程序。所以修改路径为

path = "rscript C:\Users\Charles\SkyDrive\Documents\Development\G4H\Technical evaluation templates\Graphical analysis.R"

您可能必须提供 rscript 的填充路径,具体取决于该目录是否在您的搜索路径中。

关于r - Windows 脚本宿主失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24062804/

相关文章:

r - 具有显着性星的非方形相关矩阵的相关图

r - 如何在 R 中绑定(bind)两个 xts 数据环境

excel - MS Excel 上的简单 VlookUp 不起作用

python - 如何有效地在列表末尾添加逗号?

excel - 将不同文件中的多个工作表中的数据导入单个工作簿

excel - 让宏动态调用工作簿

excel - 清除单元格内容

r - 根据特定标记从数据框中拆分字符列

r - 不使用 data.frame 的数值透明查找表?

Excel VBA : How to only allow macro to make cell input