4

I'm currently trying to run an R script from the command line (my end goal is to execute it as the last line of a python script). I'm not sure what a batch file is, or how to make my R script 'executable'. Currently it is saved as a .R file. It works when I run it from R.
How do I execute this from the windows command prompt line? Do i need to download something called Rscript.exe? Do I just save my R script as an .exe file? Please advise on the easiest way to achieve this.
R: version 3.3 python: version 3.x os: windows

W Anderson
  • 53
  • 1
  • 5
  • if it works when you click on the icon in the windows explorer, then a simple `os.system` would work, or a `subprocess.Popen` with the `shell=True` option set. Of course, creating an exe from your script would work right away with the 2 methods I just described, but it may be cumbersome in some test-and-modify phases of your script. – Jean-François Fabre Aug 09 '16 at 16:44

3 Answers3

4

As mentioned, Rscript.exe the automated executable to run R scripts ships with any R installation (usually located in bin folder) and as @Dirk Eddelbuettel mentions is the recommended automated version. And in Python you can run any external program as a subprocess with various types including a call, check_output, check_call, or Popen and the latter of which provides more facility such as capturing errors in the child process.

If R directory is in your PATH environmental variable, you do not need to include full path to RScript.exe but just name of program, Rscript. And do note this is fairly the same process for Linux or Mac operating systems.

command = 'C:/R-3.3/bin/Rscript.exe'          # OR command = 'Rscript'
path2script = 'C:/Path/To/R/Script.R'
arg = '--vanilla'

# CHECK_CALL VERSION
retval = subprocess.check_call([command, arg, path2script], shell=True)

# CALL VERSION
retval = subprocess.call(["'Rscript' 'C:/Path/To/R/Script.R'"])

# POPEN VERSION (W/ CWD AND OUTPUT/ERROR CAPTURE)
curdir = 'C:/Path/To/R/Script'
p = subprocess.Popen(['Rscript', 'Script.R'], cwd=curdir,
                     stdin = subprocess.PIPE, stdout = subprocess.PIPE, 
                     stderr = subprocess.PIPE)            
output, error = p.communicate()

if p.returncode == 0:            
    print('R OUTPUT:\n {0}'.format(output.decode("utf-8")))
else:                
    print('R ERROR:\n {0}'.format(error.decode("utf-8"))) 
Community
  • 1
  • 1
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • thanks for the reply. I tried the "CHECK_CALL VERSION" line and it says that my command returned a non-zero exit status 1. When I ran Rscript.exe full\path\rfile.R in the command prompt it worked perfectly. Note that my R script does not have any return line, it only edits a powerpoint presentation with the R2PPT/RDCOMClient packages. – W Anderson Aug 09 '16 at 17:35
  • Carefully check the paths. As commented, use `command = 'Rscript'` (.exe not needed) instead of full path as your command prompt indicates you have R in `PATH` variable. And because no output is returned, nothing on Python console should appear. – Parfait Aug 09 '16 at 19:05
3

You already have Rscript, it came with your version of R. If R.exe, Rgui.exe, ... are in your path, then so is Rscript.exe.

Your call from Python could just be Rscript myFile.R. Rscript is much better than R BATCH CMD ... and other very old and outdated usage patterns.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • could you elablorate please? do i need to import anything in python in order to do this? am i to use subprocess.Popen as mentioned above? could you give an exmaple of what to type in python – W Anderson Aug 09 '16 at 16:53
  • You need to adjust your PATH so that the notebook knows where the R binaries are. The `popen()` and `system()` etc pp will all be able to call it ----- that is pretty much _exactly_ what PATH is for. How to adjust the PATH on Windows has been explained numerous times before. – Dirk Eddelbuettel Aug 09 '16 at 17:17
1

You probably already have R, since you can already run your script.

All you have to do is find its binaries (the Rscript.exe file).

Then open windows command line ([cmd] + [R] > type in : "cmd" > [enter])

Enter the full path to R.exe, followed by the full path to your script.

Loïc
  • 11,804
  • 1
  • 31
  • 49
  • I have the program R. I also am using a jupyter notebook to run python scripts. could you please give an example of exactly what to type into the windows command prompt for me to run an r script? – W Anderson Aug 09 '16 at 17:00
  • it depends where RScript is. First you have to find the binaries in your system, maybe make a search in your R Installation folder for Rscript.exe) – Loïc Aug 09 '16 at 17:06
  • maybe though it's already in the PATH, so try just to type : Rscript.exe in the terminal – Loïc Aug 09 '16 at 17:08
  • I get the output : 'Rscript.exe' is not recognizable as an internal or external command, operable program or batch file when i input: Rscript.exe into the windows command prompt. I assume it is not in the path but I'm not sure where it is. currently searching for it. – W Anderson Aug 09 '16 at 17:11
  • Maybe in R code editor's options you can have the full path to the interpretor. – Loïc Aug 09 '16 at 17:12
  • 1
    thanks, I found where Rscript was and now can use -- Rscript.exe full\path\rfile.R in the command prompt – W Anderson Aug 09 '16 at 17:24