0

I am trying to run an R script that is in the same directory as the python script while executing the python script.

So far, I have:

if condition is True:
    import subprocess
    subprocess.call (["C:/Program Files/R/R-3.4.3/Rscript", "./testing.r"])
    sys.exit()

I keep getting the error:

OSError: [WinError 193] %1 is not a valid Win32 application

I've tried replacing "C:/Program Files/R/R-3.4.3/Rscript" with "/usr/bin/Rscript" but keep getting the same error. I was wondering if anybody would know why it keeps throwing this error?

4
  • Are sure this shouldn’t be submitted with cmd? Commented May 18, 2018 at 2:16
  • Try this subprocess.call(['C:/Program Files/R/R-3.4.3/Rscript', '--vanilla', 'testing.r'], shell = True) or provide the full path to the R script file Commented May 18, 2018 at 4:20
  • I guess the it might be a path problem. (The R version and Rscript path is for sure correct?) If yes, then give the full absolute path to testing.r, so 'C:/full/absolute/path/to/testing.r' (or is it in 'D:/' even)? Commented May 19, 2018 at 18:54
  • I've tried both suggestions but get an error that "C:/Program Files/R/R-3.4.3/Rscript" is not recognized as an external or internal command. Commented May 20, 2018 at 1:07

1 Answer 1

2

I believe the arguments of subprocess.call get passed straight to the command line, so you need to escape your quotation marks as such "\"C:/Program Files/R/R-3.4.3/Rscript\"". That being said, I get a [WinError 5] Access violation error when I use this. A workaround is to use the executable argument:

import sys
import subprocess

if True is True:
    subprocess.call(["C:/Program Files/R/R-3.4.3/Rscript.exe", "./testing.r"], 
                    executable="C:/Program Files/R/R-3.4.3/Rscript.exe")
    sys.exit()

Also, make sure that C:/Program Files/R/R-3.4.3/Rscript.exe is the location of your Rscript.exe. Mine is C:/Program Files/R/R-3.4.3/bin/Rscript.exe.

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

1 Comment

Yes, I was missing the "bin" in "C:/Program Files/R/R-3.4.3/bin/Rscript.exe". Thanks!

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.