Execution of R program as a bash script

Here is a simple example of bash script which executes R code within. Create a file called R.sh with the following content:

#!/bin/bash
R --no-save < # Start R function rfunction
rfunction<-function(num1,num2) {
# R Function definition
print(num1+num2)
}
# R function execution with 2 bash arguments
rfunction($1,$2)
EOF

make a file executable by command:
chmod +x R.sh
run script:
./R.sh 2 5
run script in the bash background:
./R.sh 2 5 &
run script in the bash background with nohup so you can leave the session and the program will still run:
nohup ./R.sh 2 5 &
OUTPUT:
~$ ./R.sh 2 5

R version 2.7.1 (2008-06-23)
Copyright (C) 2008 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> # Start R funstion rfunction
> rfunction<-function(num1,num2) {
+ # R Function definition
+ print(num1+num2)
+ }
> # R function execution with 2 bash arguments
> rfunction(2,5)
[1] 7
>

Comments are closed.