Introduction to R
NPTEL NOC18-CS28
Data science for Engineers
In this lecture
R and RStudio
How to
◦ set the working directory
◦ create an R file and save it
◦ execute an R file
◦ execute pieces of code
R Basics NPTEL NOC18-CS28 2
Data science for Engineers
R
Open source programming
language
Statistical software and Data
analysis tool
Interface
◦ Command line user interface
Platforms
◦ Windows, Linux and Mac OS
R Basics NPTEL NOC18-CS28 3
Data science for Engineers
RStudio
Integrated Development Environment
(IDE) for R
Availability
◦ Open source
◦ Commercial
Editions
◦ Desktop
◦ Server
Platforms
◦ Windows, Linux and Mac OS
R Basics NPTEL NOC18-CS28 4
Data science for Engineers
A first look of RStudio
Environment/History
Files/Plots/Packages/Help
Console
R Basics NPTEL NOC18-CS28 5
Data science for Engineers
Set the working directory
Enter in the console : setwd( “directorypath” )
(OR)
To choose Working
directory
STEP 1: Choose a suitable location by clicking on the indicated icon
R Basics NPTEL NOC18-CS28 6
Data science for Engineers
Set the working directory
STEP 2: Once directory is chosen, select the more icon and choose “Set as Working Directory”
R Basics NPTEL NOC18-CS28 7
Data science for Engineers
RStudio
R Basics NPTEL NOC18-CS28 8
Data science for Engineers
Create an R file
“File” icon
R Basics NPTEL NOC18-CS28 9
Data science for Engineers
Create an R file
By clicking the icon “ ”below the toolbar
R Basics NPTEL NOC18-CS28 10
Data science for Engineers
RStudio with script file open
Scripts
Environment/History
Files/plots/packages/Help
Console
R Basics NPTEL NOC18-CS28 11
Data science for Engineers
Writing scripts
R Basics NPTEL NOC18-CS28 12
Data science for Engineers
Saving R script file
R Basics NPTEL NOC18-CS28 13
Data science for Engineers
Saving R script file
R Basics NPTEL NOC18-CS28 14
Data science for Engineers
Executing an R file
A) Press Run/ Ctrl + Enter
B) Press Source / Ctrl + Shift + S
Or
C) Press Ctrl + Shift + Enter (Source with echo)
Source will execute all commands in console
without displaying them where as source with
echo will execute and print automatically
R Basics NPTEL NOC18-CS28 15
Data science for Engineers
Executing an R file: Example
RESULT
R Basics NPTEL NOC18-CS28 16
Data science for Engineers
Execute pieces of code – Run
R Basics NPTEL NOC18-CS28 17
Data science for Engineers
Execute pieces of code – Run
Value of ‘a’ has changed but not ‘b’
R Basics NPTEL NOC18-CS28 18
Data science for Engineers
Executing R files – Summary
Run can be used to execute
selected lines
Source/ Source with echo is for a
whole file
Advantages – using Run :
◦ troubleshooting/debugging
Disadvantages – using Run :
◦ For large section, console will be over
populated and messy
R Basics NPTEL NOC18-CS28 19