## Possibilities for Executing R Scripts

### R console

source("my_script.R")


### Command-line

Rscript my_script.R # or just ./myscript.R after making it executable
R CMD BATCH my_script.R # Alternative way 1
R --slave < my_script.R # Alternative way 2


### Passing arguments from command-line to R

Create an R script named test.R with the following content:

myarg <- commandArgs()
print(iris[1:myarg[6], ])


Then run it from the command-line like this:

Rscript test.R 10


In the given example the number 10 is passed on from the command-line as an argument to the R script which is used to return to STDOUT the first 10 rows of the iris sample data. If several arguments are provided, they will be interpreted as one string and need to be split in R with the strsplit function. A more detailed example can be found here.

Previous Page                     Next Page