Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run R script from .bat (batch file)

Tags:

r

batch-file

I am trying to run a r script using batch file. Currently I am using start"" "shortcut of R" to open R. However, I wanna R automatically run a r script that I have saved on the computer.

Possibly R will be closed after running the script and user do not see R running.

Is that possible? Thanks a lot!

like image 631
Wendan Zhong Avatar asked Jul 23 '13 13:07

Wendan Zhong


People also ask

How do I run an Rscript in Windows?

After you add Rscript to the path, in my case by adding the folder C:\Program Files\R\R-4.0. 2\bin\x64 to the path, be sure to open and close CMD for PATH to refresh. Then open a new window of CMD, and type Rscript , and tell me your output.

How do I run an Rscript?

To run an R command, put the cursor on the line of the command and then click the Run button at the top of the file window. Or just press CTRL-Enter. In the next figure data was read, and a histogram created, with the lessR Histogram function.

What is %% R in batch command?

/r - iterate through the folder and all subfolders, i.e. recursively. %%I - placeholder for a path/filename. The above command simply lists all files in the current folder and all subfolders. Follow this answer to receive notifications.


1 Answers

Rscript is a non-interactive variant of the standard R command, just designed for this kind of use.

For example, under windows you can define a launcher.bat like this :

PATH PATH_TO_R/R-version/bin;%path%
cd PATH_TO_R_SCRIPT
Rscript myscript.R arg1 arg2

In myscript.R you add the code to read the arguments:

args <- commandArgs(trailingOnly = TRUE)
arg1 <- as.character(args[1])  
arg2 <- as.numeric(args[2])  
like image 154
agstudy Avatar answered Oct 01 '22 00:10

agstudy