How to Run a R Language(.R) File Using Batch File

How to run a R language(.r) file using Batch file,i need it for jenkins automation

In text file write
"C:\Program Files\R\R-3.3.1\bin\i386\Rscript.exe" finalR.R and save it as xxx.bat file in desktop,save the finalR.R in same location(desktop).now double click the bat file it works.

Make a R script executable by sourcing it in a batch file

Found the answer : I tried to use R interactively via batch mode, which obviously couldn't work as batch is non-interactive.

I nevertheless found a workaround thanks to this answer : https://stackoverflow.com/a/11567220/9765404, consisting in downloading R-Portable with all libraries used by my project installed locally and adding this function to the Rprofile.site file (in App/R-Portable/etc) :

.First = function(){ #.First() is the first function R-Portable will execute when opened
.libPaths(.Library) #force R-Portable to use the local library
source("path-to-file") #launches the program
}

Then you can just export the folder containing R-Portable and your project to any Windows computer and execute R-Portable.exe, it works without having R installed.

Run R script from command line

If you want the output to print to the terminal it is best to use Rscript

Rscript a.R

Note that when using R CMD BATCH a.R that instead of redirecting output to standard out and displaying on the terminal a new file called a.Rout will be created.

R CMD BATCH a.R
# Check the output
cat a.Rout

One other thing to note about using Rscript is that it doesn't load the methods package by default which can cause confusion. So if you're relying on anything that methods provides you'll want to load it explicitly in your script.

If you really want to use the ./a.R way of calling the script you could add an appropriate #! to the top of the script

#!/usr/bin/env Rscript
sayHello <- function(){
print('hello')
}

sayHello()

I will also note that if you're running on a *unix system there is the useful littler package which provides easy command line piping to R. It may be necessary to use littler to run shiny apps via a script? Further details can be found in this question.

Passing Parameters to an R Script via CMD or Batch

You can do all of this in R using one of these packages to parse command-line options:

  • docopt (my favourite)
  • optparse
  • argparse
  • getopt

or doing it manually -- not recommended.

You also do not want the older R CMD BATCH -- use Rscript (or littler, but littler does not work on Windows).

Code Example

#!/usr/bin/Rscript

suppressMessages(library(docopt))

doc <- "Usage: foo.R [-h] [-x] [--src REPODIR] [--out OUTDIR] [FILES...]

-s --src REPODIR source root directory [default: ~/git]
-o --out OUTDIR output directory [default: /tmp]
-h --help show this help text"

opt <- docopt(doc) # docopt parsing

print(opt)

Use with -h

You get a nice message, automatically, with not formatting need:

edd@rob:/tmp$ Rscript so50256138.R -h
Usage: foo.R [-h] [-x] [--src REPODIR] [--out OUTDIR] [FILES...]

-s --src REPODIR source root directory [default: ~/git]
-o --out OUTDIR output directory [default: /tmp]
-h --help show this help text
edd@rob:/tmp$

Use with argument

Note how one default argument is used, and the other from the command-line:

edd@rob:/tmp$ Rscript so50256138.R -s A 
List of 9
$ --src : chr "A"
$ --out : chr "/tmp"
$ --help: logi FALSE
$ -x : logi FALSE
$ FILES : list()
$ src : chr "A"
$ out : chr "/tmp"
$ help : logi FALSE
$ x : logi FALSE
NULL

You can access them in opt by name or by option flag.

The docopt site has more; this is actually a portable specification and the CRAN package implements it for R.

Call a function in R Script from a Batch file

Here is an example. Here is the Rscript that i have, i tend to save them as txt itself.

## Rscript: Passing arguments to the Rscripts likes args[i] 
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)
print(1:args[1])
df=data.frame(1:args[1])
write.csv(df, args[2])

Then your batch file would look like this. Then you feed those arguments either directly to a cmd or create a batch file out of it.

echo off
Rscript rparam.txt 1000 out.csv

For your case, your Rscript(R_with_par.R) would be:

#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)
x1=args[1]
x2=args[2]
x3=args[3]
PNLCalcMultipleDatesClient <- function(begindate, enddate, Client)
{
# Do some operation here....
.....
......

}

PNLCalcMultipleDatesClient(as.Date(x1), as.Date(x2), as.character(x3))

And your CMD command would be:

Rscript R_with_par.R 2010-10-03 2010-10-05 Dunavant

You have to make sure that parameters that you pass are in format required by R. Give the path of the R script if you are not in the same directory. Also Rscript is far better choice than R CMD.

When trying to run R script from CMD, getting Fatal error: cannot open file 'CMD': No such file or directory

As Thomas pointed out, I needed to change "Rscript.exe" to "R.exe". For future googlers, here is the line of code that worked:

"C:\Program Files\R\R-3.2.3\bin\R.exe" CMD BATCH "C:\Users\<my username>\Documents\R\Script Testing\script.R"

R Run knitr from a batch-file (windows)

I use something along the lines

Rscript -e "require ('knitr'); knit ('test.Rmd')"


Related Topics



Leave a reply



Submit