简体   繁体   中英

Error while running R script in Python with SubProcess

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM