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

北城余情 提交于 2020-01-11 04:08:16

问题


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')

回答1:


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:


After running test_r.bat:


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.




回答2:


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:

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



来源:https://stackoverflow.com/questions/28263741/unable-to-run-r-script-through-bat-files-in-windows-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!