# Test basic arithmetic
2 + 2
# Check R version
R.version.string
# Get help
help(mean)
Installing R and RStudio
Introduction
R is a powerful programming language and free software environment for statistical computing and graphics. In this guide, we’ll walk you through installing R and RStudio, which together provide everything you need to start your journey in data analysis and statistical computing.
Why R and RStudio?
Before diving into installation, let’s understand what we’re installing:
R is the core programming language that provides:
- Comprehensive statistical and graphical capabilities
- Extensive package ecosystem (CRAN)
- Active community and continuous development
- Free and open-source
RStudio is an Integrated Development Environment (IDE) that makes working with R much easier by providing:
- Syntax highlighting and code completion
- Integrated help and documentation
- Project management tools
- Built-in plotting and data viewing
- Package management interface
Think of R as the engine and RStudio as the comfortable cockpit that makes driving the engine a pleasant experience.
Installation Steps
Step 1: Install R
R must be installed first, as RStudio depends on it.
For Windows:
- Go to the CRAN website
- Click “Download R for Windows”
- Click “base” (for first-time users)
- Download the latest version (currently R 4.4.x)
- Run the downloaded installer and follow the prompts
- Use default settings unless you have specific preferences
For macOS:
- Go to the CRAN website
- Click “Download R for macOS”
- Download the appropriate installer:
- For Apple Silicon Macs (M1/M2): R-4.4.x-arm64.pkg
- For Intel Macs: R-4.4.x-x86_64.pkg
- Open the downloaded .pkg file and follow the installation steps
For Linux:
Most Linux distributions include R in their package repositories. For Ubuntu/Debian:
sudo apt update
sudo apt install r-base r-base-dev
For more detailed Linux instructions, visit the CRAN Linux page.
Step 2: Install RStudio Desktop
After installing R successfully:
- Go to the RStudio download page
- Scroll down to “All Installers and Tarballs”
- Choose the appropriate installer for your operating system:
- Windows:
.exe
installer - macOS:
.dmg
installer - Linux:
.deb
or.rpm
package
- Windows:
- Download and run the installer
- Follow the installation prompts
Verifying Your Installation
Let’s make sure everything is working correctly:
Test R Installation
- Open RStudio (it should automatically find your R installation)
- In the Console pane (usually bottom-left), you should see something like:
R version 4.4.1 (2024-06-14) -- "Race for Your Life"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
...
Type 'q()' to quit R.
>
- Try a simple command:
If these work without errors, congratulations! Your R installation is successful.
Essential Configuration
Setting Your Working Directory
It’s helpful to organize your R work in a dedicated folder:
# Check current working directory
getwd()
# Set working directory (adjust path as needed)
setwd("~/Documents/R-Projects")
# Or better yet, use RStudio Projects (covered in next module)
Installing Essential Packages
R’s power comes from its packages. Let’s install some essential ones:
# Install the tidyverse (collection of data science packages)
install.packages("tidyverse")
# Install other useful packages
install.packages(c("here", "rmarkdown", "devtools"))
# Load a package to test
library(tidyverse)
Package Management Fundamentals
Understanding package management is crucial for R success:
CRAN Packages
# Install from CRAN (most common)
install.packages("package_name")
# Install multiple packages
install.packages(c("dplyr", "ggplot2", "readr"))
# Update packages
update.packages()
# Remove packages
remove.packages("package_name")
Loading Packages
# Load package for current session
library(dplyr)
# Alternative loading method
require(dplyr)
# Check installed packages
installed.packages()
# Check loaded packages
search()
RStudio Layout Overview
When you first open RStudio, you’ll see four panes:
- Console (bottom-left): Where R commands are executed
- Source (top-left): Text editor for scripts and documents
- Environment/History (top-right): Variables and command history
- Files/Plots/Packages/Help (bottom-right): File browser, plots, package manager, and help
Don’t worry if this seems overwhelming – we’ll explore each pane in detail in the next lesson!
Common Installation Issues
“R not found” Error in RStudio
- Solution: Reinstall R first, then RStudio
- RStudio needs to detect your R installation
Package Installation Fails
- Windows: Install Rtools from CRAN
- macOS: Install Xcode command line tools:
xcode-select --install
- All platforms: Try installing packages from a different mirror
Permission Issues
- Solution: Run RStudio as administrator (Windows) or use
sudo
(Linux/macOS) for package installation
Alternative Installation Methods
Using Package Managers
Windows - Chocolatey:
choco install r.project rstudio
macOS - Homebrew:
brew install --cask r rstudio
Linux - Conda:
conda install -c r r-base rstudio
Next Steps
Now that you have R and RStudio installed:
- Explore the RStudio Interface
- Try R as a Calculator
- Learn about R Scripts and Projects
Resources for Further Learning
Congratulations!
You’ve successfully set up your R environment! You’re now ready to start your data science journey with R.