无法通过Windows Server中的.bat文件运行R脚本 [英] Unable to run R script through .bat files in Windows Server
问题描述
我正在尝试通过.bat文件运行R脚本.当我自己运行命令行时,它可以工作.但是当我尝试运行.bat文件时,它不起作用.
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.
这是.bat文件
cd "C:\Program Files\R\R-3.1.2\bin"
R CMD BATCH "C:\Users\Administrator\Downloads\testa_vps.R"
这是R脚本
setwd('C:\Users\Administrator\Documents')
file.create('mycsv.csv')
推荐答案
我不是Windows方面的专家,因此通常会尝试使用类似Unix的系统,但是我发现非交互地使用程序当您将适当的目录添加到(用户)PATH
变量时,而不是cd
进入目录并从.bat
文件中调用可执行文件时,(例如,通过.bat
文件)通常不那么容易出错.例如,除其他外,我的用户PATH
变量包含C:\PROGRA~1\R\R-3.0\bin\;
-包含R.exe
和Rscript.exe
的目录-(其中PROGRA~1
是Program Files
的别名,您可以在未加引号的位置使用它文件路径,因为名称中没有空格).
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 cd
ing 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).
执行完此操作后,可以通过在新的命令提示符中键入Rscript
来检查PATH
修改是否成功-它应打印出Rscript
的用法信息,而不是典型的xxx is not recognized as an internal or external command...
错误消息.
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.
在目录C:\Users\russe_000\Desktop\Tempfiles
中,我创建了test_r_script.r
,其中包含
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")
和test_r.bat
,其中包含
Rscript --vanilla --no-save "C:\Users\russe_000\Desktop\Tempfiles\test_r_script.r"
单击Windows批处理文件test_r
成功运行了该过程,并在正确的文件夹中生成了mycsv.csv
.
Clicking on the Windows Batch File test_r
ran the process successfully and produced mycsv.csv
in the correct folder.
在运行test_r.bat
之前:
运行test_r.bat
后:
我从来没有使用过Windows服务器,但是我不明白为什么该过程与在个人计算机上会有根本的不同.如果您没有足够的特权来更改环境变量,则可能只需要系统管理员修改PATH
变量即可.
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.
这篇关于无法通过Windows Server中的.bat文件运行R脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!