Skip to main content
R Programming
CHAPTER 02 Beginner

Installing R and RStudio

Updated: May 18, 2026
5 min read

# CHAPTER 2

Installing R and RStudio

1. Chapter Introduction

A properly configured R environment is the foundation of productive data science work. This chapter installs R, configures RStudio, and verifies the complete setup — from first launch to installing the tidyverse.

2. Installing R

text
1234567891011121314
Step 1: Download R from CRAN
  → https://cran.r-project.org/
  → Choose your OS: Windows / macOS / Linux

Step 2: Run installer
  Windows: R-4.x.x-win.exe → Next → Accept defaults
  macOS:   R-4.x.x.pkg     → Open → Agree → Install

Step 3: Verify R installation
  Open Terminal/Command Prompt:
  $ R --version
  R version 4.3.2 (2023-10-31) -- "Eye Holes"

CURRENT VERSION: R 4.3.x (check cran.r-project.org for latest)

3. Installing RStudio

text
1234567891011
Step 1: Download RStudio Desktop (FREE)
  → https://posit.co/download/rstudio-desktop/
  → RStudio Desktop (Free) → Download for your OS

Step 2: Install RStudio
  Windows: RStudio-2023.xx.exe → Run installer
  macOS:   RStudio-2023.xx.dmg → Drag to Applications

Step 3: First launch
  RStudio automatically detects your R installation.
  File → New File → R Script (Ctrl+Shift+N)

4. RStudio Interface

text
12345678910111213141516171819202122232425
RStudio 4-Panel Layout:

┌──────────────────────────────┬──────────────────────────────────────┐
│  SOURCE EDITOR (Top-Left)    │  ENVIRONMENT / HISTORY (Top-Right)   │
│  • R scripts (.R files)      │  • Objects in memory                 │
│  • R Markdown (.Rmd)         │  • Variable values and types         │
│  • Run: Ctrl+Enter (line)    │  • Command history                   │
│  • Run: Ctrl+Shift+Enter     │  • Connections tab                   │
│    (entire script)           │                                      │
├──────────────────────────────┼──────────────────────────────────────┤
│  CONSOLE (Bottom-Left)       │  FILES / PLOTS / HELP (Bottom-Right) │
│  • Interactive R session     │  • File browser                      │
│  • Type R code directly      │  • Plot viewer                       │
│  • See output immediately    │  • Package manager                   │
│  • > prompt = ready          │  • Help documentation                │
└──────────────────────────────┴──────────────────────────────────────┘

Keyboard Shortcuts:
  Ctrl+Enter       → Run current line
  Ctrl+Shift+Enter → Run entire script
  Alt+-            → Insert assignment operator (<-)
  Ctrl+Shift+M     → Insert pipe (%>%)
  Ctrl+L           → Clear console
  F1               → Help for selected function
  Ctrl+Shift+C     → Comment/uncomment selection

5. Your First R Script

r
123456789101112131415161718192021222324252627
# My First R Script
# Run each line with Ctrl+Enter

# Print to console
print("Hello, R World!")
cat("Welcome to R Programming!\n")

# Basic arithmetic
2 + 2
sqrt(144)
pi * 5^2   # Area of circle with radius 5

# Create variables
name <- "Alice"        # <- is assignment operator in R
age  <- 28
score <- 95.5

# Print with paste
cat("Name:", name, "\n")
cat("Age:", age, "\n")
cat("Score:", score, "\n")

# Check class of object
class(name)    # "character"
class(age)     # "numeric"
class(score)   # "numeric"
class(TRUE)    # "logical"

6. Installing and Loading Packages

r
123456789101112131415161718192021222324252627282930
# Install a package (only once)
install.packages("tidyverse")
install.packages("ggplot2")
install.packages(c("dplyr", "readr", "tidyr"))  # Multiple at once

# Load package for use in this session
library(tidyverse)
library(ggplot2)

# Check installed packages
installed.packages()[, "Package"]  # List all installed packages

# Update all packages
update.packages(ask = FALSE)

# Check package version
packageVersion("ggplot2")  # '3.4.4'

# Get help for a function
?mean          # Open help documentation
help("ggplot") # Alternative

# Verify tidyverse is working
glimpse(mtcars)  # Built-in dataset

# Install for data science (do this once after installing R)
essential_packages <- c("tidyverse", "ggplot2", "dplyr", "readr", "caret",
                          "lubridate", "stringr", "forcats", "purrr", "broom")
install.packages(essential_packages)
cat("✅ All essential packages installed!\n")

7. R Projects (Best Practice)

r
123456789101112131415
# Always work in R Projects for reproducibility
# File → New Project → New Directory → New Project
# Give it a name like "my_analysis"

# This creates:
#   my_analysis/
#   ├── my_analysis.Rproj  ← Double-click to open project
#   ├── data/              ← Put your CSV/Excel files here
#   ├── scripts/           ← Your .R files
#   └── output/            ← Plots and reports

# Set working directory (alternative to Projects)
setwd("C:/my_project")  # Windows
getwd()                  # Check current directory
list.files()             # See files in directory

8. Common Mistakes

  • Using = instead of <- for assignment: In R, the convention is x <- 5. While = also works, <- is standard R style and avoids ambiguity inside function calls.
  • Forgetting library() after installation: install.packages() downloads the package once. You must call library(package) at the start of every R session to use it.

9. MCQs

Question 1

R is downloaded from?

Question 2

RStudio Source Editor is where you?

Question 3

library(ggplot2) does?

Question 4

install.packages() needs to be called?

Question 5

R assignment operator convention?

Question 6

getwd() returns?

Question 7

Ctrl+Enter in RStudio runs?

Question 8

RStudio Environment panel shows?

Question 9

R Projects (.Rproj) provide?

Question 10

?mean in console opens?

10. Interview Questions

  • Q: What is the difference between install.packages() and library()?
  • Q: Why are R Projects recommended over setwd()?

11. Summary

Install R from CRAN, RStudio from Posit. RStudio 4-panel layout: Source (scripts), Console (REPL), Environment (objects), Files/Plots/Help. Assignment operator: <-. Package workflow: install.packages() once, library() every session. Use R Projects for reproducible directory management. Install tidyverse as the essential data science suite.

12. Next Chapter Recommendation

In Chapter 3: R Syntax and Basics, we learn R's fundamental syntax — comments, variables, output, and build a basic calculator application.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·