User Tools

Site Tools


Scripting, if else condition, loops and plotting graphics

R script

An R script is a text file containing a list of R commands that you would enter in the command line of R. Using scripting is useful to carry out stand alone processes and eventually multi-tool tasks within data processing flows that include different applications to take part. Scripting is also required for cluster processing. To create an R script you have to write a .txt file and save it with the .R extension.

To run a script, let's say one with the name:


you may use either:


on the command line of R or you can directly run the script from the Bash shell using the following command

  R CMD BATCH /home/path/to/myscript/myscript.R 


 R  --no-save  -q <     ost4sem/exercise/basic_adv_r/advance_synecology.R 

R script example

We create a txt file with the following R commands and save it as myscript.R

  i <- 1:10
  outer(i, i, "*")

then we lance it from a Bash terminal and we specify to –no-save the R working space once the R terminal will close

  R<sink.R --no-save

We can check the result of the output. A myscriptoutputexample.txt should be available in the working direcory since the R sink function is used to divert R output to a connection, and in our example to the output text file. ! remember to clean up your directory once finished

  source("rm /home/path/to/myscript/myscript.R")

if else condition

if (logic test) {TRUE} else {FALSE}

if (data$j==9) {ALPHA=0} else {ALPHA=data$j/45+data$t}

ifelse(test, yes, no) returns a value with the same shape as test, which is filled with elements selected from either yes or no depending on whether the element of the test is TRUE or FALSE.

test an object which can be coerced to logical mode
yes return values for true elements of test
no return values for false elements of test


  x <- c(10:-11)
  x.positive = ifelse(x >= 0, x, NA)
  sqrt(x)#- gives warning
  sqrt(ifelse(x >= 0, x, NA))# no warning

For loop and plotting graphics

We use a for loop function for plotting 30 different graphics. Each graphic represents a different forest tree species and its altitude range of growth according to slope orientation. = read.csv("~/ost4sem/exercise/basic_adv_r/inputs/SPECIES_DEF.txt",header=TRUE)$name[$code==1]
FFD = advanceRdata 
for (i in c(1,5,7,10,11,13,15,17,20,22,29,35,36,41,44,46,48,49,51,54,56,68,100,116,118,123,125,129,130,134)){
  mod <- paste("S",i,sep="")
  presence = subset(FFD,FFD[[mod]]==1)
  absence = subset(FFD,FFD[[mod]]!=1)
  png(paste("~/ost4sem/exercise/basic_adv_r/outputs/altitude_png/sp_",i,"_altitude.png",sep=""),width = 800, height = 800)
## plot altitude for each Aspect 
  plot(presence$pr205,presence$pr201,$name[$code==i], col = "green3",  axes=FALSE, xlab="Slope Orientation", ylab="Mean Altitude    (m)", axis=FALSE, cex.main=3, cex.lab=2)
  axis(1, at=c(1,2,3,4), labels=c("North","South","East","West"), cex.axis=1.5)

see for more graphics examples
wiki/loopsr.txt · Last modified: 2021/01/20 20:36 (external edit)