Filipe Ferminiano
Filipe Ferminiano

Reputation: 8799

Unable to run R script through .bat files in Windows Server

I'm trying to run a R script through a .bat file. When I run myself the commands line by line it works. But when I try to run the .bat file, it doesn't works.

This is the .bat file

cd "C:\Program Files\R\R-3.1.2\bin"
R CMD BATCH "C:\Users\Administrator\Downloads\testa_vps.R"

This is the R script

setwd('C:\Users\Administrator\Documents')
file.create('mycsv.csv')

Upvotes: 2

Views: 4649

Answers (2)

alex23lemm
alex23lemm

Reputation: 5675

As already suggested by @nrussel in the comments you should use RScript.exe for this.

Create a file launcher.bat with the following content:

cd C:\Users\Administrator\Documents
Rscript testa_vps.R

In addition, add C:\Program Files\R\R-[your R version]\bin\x64; or C:\Program Files\R\R-[your R version]\bin\i386to the System PATH variable in the Environment Variables menu depending if you run R on a 64-bit or 32-bit system.

I just tested the approach above successfully on a Windows Server 2008 64-bit system and mycsv.csv got created as expected.

EDIT

One important point I forgot to mention is the following: You need to specify the path in your R file in the setwd() call using \\ instead of \.

setwd('C:\\Users\\Administrator\\Documents')

Here is a screenshot of the successful run on the Windows 2008 server:

enter image description here

Note: I added cmd /k to the .bat file so that the cmd window stays open after clicking on the file.

Upvotes: 0

nrussell
nrussell

Reputation: 18612

I'm not an expert with Windows and generally try to stick to Unix-like systems for things like this, but I have found that using programs non-interactively (e.g. via .bat files) is usually less error-prone when you add the appropriate directories to your (user) PATH variable, rather than cding into the directory and calling the executable from within the .bat file. For example, among other things, my user PATH variable contains C:\PROGRA~1\R\R-3.0\bin\; - the directory that contains both R.exe and Rscript.exe - (where PROGRA~1 is an alias for Program Files which you can use in an unquoted file path, since there are no spaces in the name).

After you do this, you can check that your PATH modification was successful by typing Rscript in a new Command Prompt - it should print out usage information for Rscript rather than the typical xxx is not recognized as an internal or external command... error message.

In the directory C:\Users\russe_000\Desktop\Tempfiles, I created test_r_script.r, which contains

library(methods)
setwd("C:\Users\russe_000\Desktop\Tempfiles")
file.create("mycsv.csv")

and test_r.bat, which contains

Rscript --vanilla --no-save "C:\Users\russe_000\Desktop\Tempfiles\test_r_script.r"

Clicking on the Windows Batch File test_r ran the process successfully and produced mycsv.csv in the correct folder.


Before running test_r.bat: enter image description here


After running test_r.bat: enter image description here


I've never worked with a Windows server, but I don't see why the process would be fundamentally different than on a personal computer; you just may need your sysadmin to modify the PATH variable if you don't have sufficient privileges to alter environment variables.

Upvotes: 4

Related Questions