[英]How to run an R script file from the command line
I know there are a lot of questions regarding this issue but I've tried everything and I think I just don't understand how the command line works in windows. 我知道有关此问题有很多问题,但是我已经尝试了所有方法,我想我只是不了解命令行在Windows中的工作方式。 I have a file saved in a folder on my desktop, let's say:
我有一个文件保存在桌面上的文件夹中,假设:
C:\Users\abika_000\Desktop\R models\myfile.R
Here is the directory to my R/bin or Rscript/bin: 这是我的R / bin或Rscript / bin的目录:
C:\Program Files\R\R-3.1.0\bin
I want to run this code using the cmd prompt. 我想使用cmd提示符运行此代码。 How would I go about doing this?
我将如何去做呢?
I've tried this solution from the question below but I just keep getting errors no matter what I do: 我从下面的问题中尝试了此解决方案,但是无论我做什么,我都会不断出错:
Run R script from command line 从命令行运行R脚本
What I entered: 我输入的内容:
> R CMD BATCH C:\Users\abika_000\Desktop\R models\myfile.R
> Rscript C:\Users\abika_000\Desktop\R models\myfile.R
The errors I get are: 我得到的错误是:
'R' is not recognized as an internal or external command, operable program or batch file
'Rscript' is not recognized as an internal or external command, operable program or batch file
EDIT:: 编辑::
Credit to splendour and Phil. 感谢辉煌和菲尔。 I wound up fixing it by entering:
我通过输入以下内容来修复它:
> "C:\Program Files\R\R-3.1.0\bin\"R CMD BATCH "C:\Users\abika_000\Desktop\R models\myfile.R"
I apologize for the poor question. 对于这个可怜的问题,我深表歉意。 I just wasn't sure how to use the cmd prompt /directories in trying to use R CMD BATCH or Rscript
我只是不确定如何在尝试使用R CMD BATCH或Rscript时使用cmd提示符/ directories
I think step 1, if you haven't already done it, is to use a UNIX-y shell emulator, such as Cygwin . 我认为第一步(如果尚未完成)是使用UNIX-y shell模拟器,例如Cygwin 。 Then, assuming it's available on Windows (as it is on Linux/OSX), you can use the wrapper program
Rscript
. 然后,假定它在Windows上可用(在Linux / OSX上也是如此),则可以使用包装程序
Rscript
。
Or, if you have a UNIX-y shell emulator, you can always pipe input straight into R
, eg: 或者,如果您有UNIX-y shell模拟器,则始终可以将输入直接通过管道传递给
R
,例如:
R --no-save --no-restore < your_script.R
The executables for R
have not been added to your environmental variable PATH
. R
的可执行文件尚未添加到您的环境变量PATH
。 The error you are receiving is CMD complaining that it cannot recognize R
as an executable. 您收到的错误是CMD抱怨它无法将
R
识别为可执行文件。 Both R
and Rscript
exists under Windows. R
和Rscript
在Windows下Rscript
存在。
From the CMD, write 从CMD写入
echo %PATH% 回声%PATH%
Did you see the path where R is install there? 您看到R安装在那里的路径了吗? Probably not.
可能不是。
Where is R installed? R安装在哪里? I'm guessing something like
C:\\Program Files (x64)\\R\\R-3.1.2
我猜像是
C:\\Program Files (x64)\\R\\R-3.1.2
Follow this link for adding a path to PATH
: http://www.computerhope.com/issues/ch000549.htm 单击此链接可为
PATH
添加路径: http : //www.computerhope.com/issues/ch000549.htm
The path you want to add is not the location where R is installed, but a subdir. 您要添加的路径不是 R的安装位置,而是子目录。 Either add
C:\\Program Files (x64)\\R\\R-3.1.2\\bin
or C:\\Program Files (x64)\\R\\R-3.1.2\\bin\\x64
. 添加
C:\\Program Files (x64)\\R\\R-3.1.2\\bin
或C:\\Program Files (x64)\\R\\R-3.1.2\\bin\\x64
。
Restart a command prompt, and try to start R
from the command line (just type R
). 重新启动命令提示符,并尝试从命令行启动
R
(只需键入R
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.