The R programming can be done in two ways. We can either type the command lines on the screen inside an "R-session", or we can save the commands as a "script" file and execute the whole file inside R. First we will learn the R-session.
To start an R session, type 'R' from the command line in windows or linux OS. For example, from shell prompt '$' in linux, type
R version 3.1.1 (2014-07-10) -- "Sock it to Me" Copyright (C) 2014 The R Foundation for Statistical Computing Platform: x86_64-unknown-linux-gnu (64-bit) 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. [Previously saved workspace restored]>
Once we are inside the R session, we can directly execute R language commands by typing them line by line. Pressing the enter key terminates typing of command and brings the > prompt again. In the example session below, we declare 2 variables 'a' and 'b' to have values 5 and 6 respectively, and assign their sum to another variable called 'c':
The value of the variable 'c' is printed as,> a = 5 > b = 6 > c = a + b > c
In R session, typing a variable name prints its value on the screen.
To get help on any function of R, type help(function-name) in R prompt. For example, if we need help on "if" logic, type,
then, help lines for the "if" statement are printed.> help("if")
To exit the R session, type quit() in the R prompt, and say 'n' (no) for saving the workspace image. This means, we do not want to save the memory of all the commands we typed in the current session:
Note that by not saving the current session, we loose all the memory of current session commands and the variables and objects created when we exit R prompt. When we work in R, the R objects we created and loaded are stored in a memory portion called workspace. When we say 'no' to saving the workspace, we all these objects are wiped out from the workspace memory. If we say 'yes', they are saved into a file called ".RData" is written to the present working directory. In Linux, this "working directory" is generally the directory from where R was started through the command 'R'. In windows, it can be either "My Documents" or user's home directory. When we start R in the same currnt directory next time, the work space and all the created objects are restored automatically from this ".RData" directory.
> > a = 5 > b = 6 > c = 8 > sum = a+b+c > sum
Specific objects created in the current session can be removed using rm() command. If we specify the name of an object, it will be removed. If we just say rm(list = las()) , all objects created so far will be removed. See below:
> a = 5 > b = 6 > c = 8 > sum = a+b+c > sum
> rm(list=c("sum")) > ls()
> rm(list = ls()) > ls()
In Windows version of R, the working directory can be set from menu in R window.
When we are inside R prompt, the operation system commands will not be recognised by R. If we want to list the names of files in the current directory in which R has been started, we should use list.files() commnd to list the files. This lists all the files in the current directory. In case we need information on a specific file, use file.info("filename") command. This prints all the information about this file on the screen.