首页 文章

从VBA运行R脚本

提问于
浏览
11

如何从VBA运行R脚本?假设我有一个R脚本存储为C:\ XXX \ testR.R

我尝试使用Shell,但不太成功 .

2 回答

  • 19
    Public Sub RunRTest()
      Shell ("Rscript test.r")
    End Sub
    
  • 10

    注意小心你的文件位置,可能需要更明确的Shell dim语句....例如 . 在VB中替换这些行

    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 = """" & Cells.Range("RhomeDir") & """ """ & Cells.Range("MyRscript") & """"
    
    errorCode = shell.Run(path, style, waitTillComplete)
    

    在excel中,具有命名引用RhomeDir的单元格包含文本

    C:\ Program Files \ R \ R-3.2.3 \ bin \ x64 \ _script和

    MyRscript包含文本“C:/Documents/Rworkings/Rscripttest.s”

    注意到unix R反斜杠和.s或.r后缀和VB用“”替换“”以在路径表达式中添加双括号(加上更多的外括号来表示字符串) . 在文件名中也有空格也不是一个好主意 .

    通过搜索VBA shell找到了上面的shell命令的完整昏暗语法 .

相关问题