Introduction to R Programming Environment and RStudio
Experiment No. 1:
Introduction to R Programming Environment and RStudio
Aim
To familiarize students with the R programming environment, install and configure RStudio, install R packages, and execute basic R commands.
Learning Objectives
After completing this experiment, students will be able to:
- Install and configure R and RStudio.
- Understand the RStudio interface.
- Execute basic R commands.
- Create and run R scripts.
- Install and load R packages.
- Access package documentation and help resources.
- Manage the R workspace and working directory.
Software Requirements
- Operating System: Windows/Linux/macOS
- R (latest stable version)
- RStudio Desktop
Theory
What is R?
R is an open-source programming language and software environment widely used for:
- Statistical computing
- Data analysis
- Machine learning
- Data visualization
- Scientific research
What is RStudio?
RStudio is an Integrated Development Environment (IDE) for R that provides:
- Script editor
- Console
- Environment viewer
- Package manager
- Plot viewer
- Help system
Components of RStudio
- Source Pane
- Used to write and edit R scripts.
- Console Pane
- Executes R commands interactively.
- Environment Pane
- Displays variables, datasets, and objects.
- Files/Plots/Packages/Help Pane
- File management
- Plot visualization
- Package management
- Documentation access
Procedure
Part A: Installing R
Step 1: Download R
Visit:
Step 2: Install R
- Download the appropriate installer for your operating system.
- Follow the installation wizard.
- Verify installation by launching R.
Part B: Installing RStudio
Step 1: Download RStudio
Visit:
Step 2: Install RStudio
- Run the installer.
- Launch RStudio.
- Verify that the R console appears.
Part C: Exploring the RStudio Environment
- Open RStudio.
- Identify:
- Source pane
- Console pane
- Environment pane
- Files/Plots/Packages/Help pane
- Execute the following command in the Console:
print("Welcome to R Programming")
Output:
[1] "Welcome to R Programming"
Part D: Basic R Commands
Execute the following commands:
a <- 10
b <- 20
sum <- a + b
print(sum)
Output:
[1] 30
Check the Environment pane and observe the created variables.
Part E: Creating and Running an R Script
- Select:
File → New File → R Script
- Enter:
name <- "Student"
cat("Hello", name)
- Save as:
experiment1.R
- Run the script using:
Run Button
or
source("experiment1.R")
Part F: Installing Packages
Installing a Package from Console
install.packages("ggplot2")
Wait for successful installation.
Loading the Package
library(ggplot2)
If no error occurs, the package is loaded successfully.
Verify package information
packageVersion("ggplot2")List all functions in the package
ls(package:ggplot2)
Open help page
help(ggplot2)
or
?ggplot2
Part G: Installing Multiple Packages
install.packages(c(
"dplyr",
"tidyr",
"readr",
"plotly"
))
Load packages:
library(dplyr)
library(tidyr)
library(readr)
library(plotly)
Part H: Viewing Installed Packages
Execute:
installed.packages()
Or use:
Packages Pane → Installed Packages
Part I: Accessing Help and Documentation
Get help for a function:
help(mean)
or
?mean
Search documentation:
help.search("plot")
Part J: Working Directory Management
Check current working directory:
getwd()
Set a new working directory:
setwd("D:/R_Lab")
Verify:
getwd()
Removing Packages in R
Sometimes, installed packages are no longer needed or may need to be reinstalled due to version conflicts. R provides the remove.packages() function to uninstall packages from the system.
Syntax
remove.packages("package_name")
where package_name is the name of the package to be removed.
Example 1: Removing a Single Package
remove.packages("ggplot2")
This command removes the ggplot2 package from the default R library.
Example 2: Removing Multiple Packages
remove.packages(c("ggplot2", "dplyr", "caret"))
This command removes all three packages.
Example 3: Specifying the Library Location
remove.packages("ggplot2",
lib = "C:/Users/User/Documents/R/win-library/4.4")
This removes the package from the specified library location.
Listing Installed Packages
Before removing packages, you can view all installed packages using:
installed.packages()
or
rownames(installed.packages())
Checking Whether a Package Exists
"ggplot2" %in% rownames(installed.packages())
Output:
[1] TRUE
Removing a Loaded Package
If a package is currently loaded, detach it first:
detach("package:ggplot2", unload = TRUE)
Then remove it:
remove.packages("ggplot2")
Reinstalling a Package
After removing a package, it can be installed again using:
install.packages("ggplot2")
Sample Exercise
Create variables and perform arithmetic operations.
x <- 25
y <- 5
add <- x + y
sub <- x - y
mul <- x * y
div <- x / y
print(add)
print(sub)
print(mul)
print(div)
Result
The R programming environment and RStudio IDE were successfully explored. Packages were installed and loaded, and basic R commands and scripts were executed successfully.
Learning outcomes
- Install R and RStudio successfully.
- Navigate the RStudio environment.
- Execute basic R commands.
- Create and run R scripts.
- Install, load, and manage R packages.
- Use R help facilities and manage working directories.
Viva Questions
- What is R?
- What is the purpose of RStudio?
- Explain the major panes of RStudio.
- What is a package in R?
- Differentiate between
install.packages()andlibrary(). - How do you create an R script?
- What is the use of
getwd()? - How can you access help in R?
- What is the Environment pane used for?
- Name any three popular R packages.
Comments
Post a Comment