How to run a R language(.r) file using Batch file?

匿名 (未验证) 提交于 2019-12-03 02:00:02

问题:

I want to run a R script file (.r) using batch file.

回答1:

I don't know what .r script file is; however, you must remember that a Batch file is just an automated way to execute DOS commands, so the answer to your question is:

How do you run a R script file with a DOS command?

If you can't do that via a command line, then a Batch file can't do either...



回答2:

If R.exe is in your PATH, then your windows batch file (.bat) would simply consist of one line:

R CMD BATCH your_r_script.R 

otherwise, you need to give the path of R.exe, so for example:

"C:\Program Files\R\R-2.13.0\bin\R.exe" CMD BATCH your_r_script.R 

you can add certain input arguments to the BATCH command, such as --no-save, --no-restore

so it would be

R CMD BATCH [options] your_r_script.R 

more info on options, etc at http://stat.ethz.ch/R-manual/R-devel/library/utils/html/BATCH.html

Note: R uses the command "BATCH" to non-interactively evaluate a script located in a file. Here we are running the command "BATCH" from a windows .BAT file, but that's merely a coincidence.



回答3:

Another answer suggests using Rscript.exe, so your batch file would contain:

"C:\Program Files\R\R-3.0.2\bin\i386\Rscript.exe"  your_r_script.R pause 

You could also directly call a R command by using the -e flag. For example this batchfile tells R to set the current working directory to Documents, then it gets the working directory:

"C:\Program Files\R\R-3.0.2\bin\i386\Rscript.exe" -e setwd('Documents');getwd() pause 

EDIT May 2016

You might want to add R to the windows environment path. In a comment in this question @chase gave a link that explains how to set the path on windows 7. Once R is added to the windows path, your batch file should become simply :

Rscript.exe  your_r_script.R pause 


回答4:

I doubt you will be able to run it using a batch file.

http://www.fileinfo.com/extension/r Most known programs that use .r files do so for source code files it looks like. You will probably have to compile it using the program it was written for. I guess you could use a command line compiler from a batch file, but I don't know what language or applications you are using.

If you post the script file or give more information about it, we could probably help you better.



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