Matt K. Matt K. - 6 months ago 201
Bash Question

Error running RScript in Excel VBA

I've searched through several solutions to this same problem, but my code still doesn't work. Made sure to not have spaces in my file path, and still have it triple quoted to be sure. I get the "Method 'Run' of object 'IWshShell3' failed" error from running the macro. What could I be missing here?

Code:

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

Answer

I believe your level of quotes is incorrect. The RScript should be in quotes while the filename for the script shouldn't be. Also, I like to make sure I include the full path names in the call. Try:

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

You may need to update the path to RScript depending on the version you have installed.