无法通过Windows Server中的.bat文件运行R脚本 [英] Unable to run R script through .bat files in Windows Server

查看:173
本文介绍了无法通过Windows Server中的.bat文件运行R脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过.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.exeRscript.exe的目录-(其中PROGRA~1Program 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 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).

执行完此操作后,可以通过在新的命令提示符中键入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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆