3

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
4
  • 1
    Can you actually run Rscript from your command line? Is it accessible via your Path environment variable? Commented Apr 14, 2016 at 21:52
  • what is errorCode after it errors out? Commented Apr 14, 2016 at 21:55
  • Is the .r file extension associated with rscript.exe ? Can you omit the program and run it by association? Commented Apr 14, 2016 at 22:01
  • I am able to run the Rscript from cmd, yes. It works perfectly when done that way. I actually just tried it out now and get no error messages. Made no changes over the weekend or anything else. So bizzare. Commented Apr 19, 2016 at 14:38

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

it helped me too. Can you please tell how to send variable to R with the path too
0

it took a little effort to get this going, but the following worked for me.

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

Make sure you read about setting up the 'permissions' using the link below.

http://shashiasrblog.blogspot.com/2013/10/vba-front-end-for-r.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.