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:

  1. Install and configure R and RStudio.
  2. Understand the RStudio interface.
  3. Execute basic R commands.
  4. Create and run R scripts.
  5. Install and load R packages.
  6. Access package documentation and help resources.
  7. 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

7
  1. Source Pane
    • Used to write and edit R scripts.
  2. Console Pane
    • Executes R commands interactively.
  3. Environment Pane
    • Displays variables, datasets, and objects.
  4. 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

  1. Open RStudio.
  2. Identify:
    • Source pane
    • Console pane
    • Environment pane
    • Files/Plots/Packages/Help pane
  3. 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

  1. Select:
File → New File → R Script
  1. Enter:
name <- "Student"

cat("Hello", name)
  1. Save as:
experiment1.R
  1. 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

  1. What is R?
  2. What is the purpose of RStudio?
  3. Explain the major panes of RStudio.
  4. What is a package in R?
  5. Differentiate between install.packages() and library().
  6. How do you create an R script?
  7. What is the use of getwd()?
  8. How can you access help in R?
  9. What is the Environment pane used for?
  10. Name any three popular R packages.

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Basic R Programs to Try - Assignment 1