Installing R and RStudio
R is a powerful programming language for statistical computing and data analysis. RStudio is an integrated development environment (IDE) that makes working with R much more enjoyable and productive. Together, they form the foundation for data science work in R.
This tutorial walks you through installing both R and RStudio on your computer. We will cover installation for Windows, macOS, and Linux, then verify that everything is working correctly.
Installing R
R is available for all major operating systems. The installation process varies slightly depending on your system.
Windows
- Visit the Comprehensive R Archive Network (CRAN) at cran.r-project.org
- Click on Download R for Windows
- Click on base (or click on install R for the first time)
- Download the latest version of R (currently R-4.4.x)
- Run the downloaded .exe file and follow the installation wizard
- Accept the default settings unless you have a specific reason to change them
macOS
- Visit CRAN at cran.r-project.org
- Click on Download R for macOS
- Download the appropriate package for your Apple Silicon (M1/M2/M3) or Intel Mac:
- Apple Silicon (M1/M2/M3): Download the arm64.pkg file
- Intel Mac: Download the x86_64.pkg file
- Open the downloaded file and follow the installation instructions
Linux
On Debian-based distributions (Ubuntu, Debian), you can install R using apt:
sudo apt update
sudo apt install r-base r-base-dev
On Fedora, RHEL, or CentOS:
sudo dnf install R
On Arch Linux:
sudo pacman -S R
Installing RStudio
RStudio is the IDE that will transform your R experience. It provides a graphical interface with a code editor, console, plots pane, and many other useful features.
All Operating Systems
- Visit the Posit website (formerly RStudio) at posit.co/downloads
- Click on Download RStudio Desktop
- Choose the free RStudio Desktop option
- Download the installer for your operating system
- Run the installer and follow the instructions
For Windows, download the .exe installer. For macOS, download the .dmg file and drag RStudio to your Applications folder. For Linux, download the appropriate .deb or .rpm package.
Verifying Your Installation
After installation, let’s make sure everything is working correctly.
Opening RStudio
Launch RStudio from your applications menu or desktop shortcut. You should see a window divided into several panes:
- Source Editor (top-left): Where you write and edit R scripts
- Console (bottom-left): Where R code is executed
- Environment/History (top-right): Shows your variables and command history
- Files/Plots/Packages/Help (bottom-right): File browser, plots, package management, and help
Running Your First R Code
In the console pane, type the following and press Enter:
# Print a simple message
print("Hello, R world!")
You should see:
[1] "Hello, R world!"
Let’s try a simple calculation:
# Basic arithmetic
2 + 2
[1] 4
Now let’s create a variable and do something with it:
# Create a numeric vector
numbers <- c(1, 2, 3, 4, 5)
# Calculate the mean
mean(numbers)
[1] 3
Checking Your R Version
To verify which version of R you have installed, run:
R.version
This displays detailed information about your R installation, including the version number, platform, and operating system.
Installing Packages
One of R’s greatest strengths is its vast ecosystem of packages—collections of functions and data that extend R’s capabilities.
Installing from CRAN
The Comprehensive R Archive Network (CRAN) hosts thousands of packages. Install the popular tidyverse package:
install.packages("tidyverse")
This downloads and installs tidyverse along with all its dependencies. You only need to install a package once (unless you want to update it).
Loading a Package
Before using a package in your code, you must load it with the library() function:
library(tidyverse)
You should see a message displaying the loaded packages and any conflicts.
Understanding the RStudio Interface
Take a few minutes to explore RStudio’s interface:
The Console
The console is where your R code runs. You can type code directly here and see results immediately. The > symbol is the prompt, indicating R is ready for input.
The Source Editor
Create a new R script by clicking File > New File > R Script or pressing Ctrl+Shift+N (Cmd+Shift+N on Mac). Write your code here and run it using:
- Ctrl+Enter (Cmd+Enter on Mac): Run the current line or selected code
- Ctrl+Shift+Enter (Cmd+Shift+Enter on Mac): Run the entire script
The Environment Pane
This pane shows all variables and data frames currently in memory. Click on a variable to inspect its contents.
The Files Pane
Navigate your project directory, view plots, manage packages, and access help documentation.
Troubleshooting Common Issues
RStudio Won’t Start
If RStudio fails to launch, try these steps:
- Ensure R is properly installed by opening R directly
- Check for error messages in the startup log
- Try reinstalling both R and RStudio
Package Installation Fails
Some packages require system dependencies. On Linux, you may need to install additional libraries:
# Ubuntu/Debian
sudo apt install libcurl4-openssl-dev libxml2-dev libssl-dev
Slow Performance
If R runs slowly, ensure you have enough RAM and consider increasing R’s memory limit with:
memory.limit(size = 4000) # Windows: increase to 4GB
Next Steps
Congratulations! You have successfully installed R and RStudio. Your next tutorial in the r-fundamentals series covers R Basics: Vectors and Types, where you’ll learn about R’s fundamental data structures.
To practice, try exploring R’s built-in datasets:
# List available datasets
data()
# Load a dataset
data(mtcars)
# View the first few rows
head(mtcars)
This gives you a preview of working with data frames—one of the most common data structures in R.