Installation & Setup
Get R and RStudio installed on your system, explore the IDE, install your first packages, and run your first R script.
Step 1: Installing R from CRAN
R is distributed through the Comprehensive R Archive Network (CRAN). Visit cran.r-project.org and download the installer for your operating system.
# Windows: https://cran.r-project.org/bin/windows/base/ # macOS: https://cran.r-project.org/bin/macosx/ # Linux (Ubuntu/Debian): sudo apt update sudo apt install r-base # Linux (Fedora): sudo dnf install R
R.version.string in the console.Step 2: Installing RStudio
RStudio is the most popular IDE for R. It is developed by Posit (formerly RStudio, PBC) and provides a powerful interface for writing, running, and debugging R code.
Download RStudio Desktop (free) from posit.co/download/rstudio-desktop.
The RStudio Interface
RStudio has four main panes:
-
Source Editor (top-left)
Where you write R scripts (.R files) and R Markdown documents (.Rmd). Supports syntax highlighting, auto-completion, and code folding.
-
Console (bottom-left)
The interactive R console where you can type and execute commands directly. Output appears here.
-
Environment / History (top-right)
Shows all variables, data frames, and objects in your current R session. The History tab stores previously executed commands.
-
Files / Plots / Packages / Help (bottom-right)
Browse files, view generated plots, manage installed packages, and access documentation.
Installing Packages
R's power comes from its package ecosystem. Packages add new functions and capabilities.
# Install a package from CRAN install.packages("tidyverse") # Install multiple packages at once install.packages(c("dplyr", "ggplot2", "readr")) # Load a package into your session library(tidyverse) # Check if a package is installed require(ggplot2) # Returns TRUE/FALSE # See all installed packages installed.packages()
CRAN vs GitHub Packages
| Source | Installation | Notes |
|---|---|---|
| CRAN | install.packages("pkg") |
Stable, reviewed, tested across platforms |
| GitHub | devtools::install_github("user/repo") |
Development versions, cutting-edge features |
| Bioconductor | BiocManager::install("pkg") |
Bioinformatics and genomics packages |
R Markdown Basics
R Markdown lets you combine R code with narrative text to create reproducible documents, reports, and presentations.
---
title: "My First Report"
author: "Your Name"
output: html_document
---
## Analysis
```{r}
summary(mtcars)
```
The mtcars dataset has `r nrow(mtcars)` observations.
Running Scripts
There are several ways to run R code in RStudio:
- Run current line: Place cursor on a line and press
Ctrl+Enter(Windows/Linux) orCmd+Enter(macOS). - Run selected code: Highlight code and press
Ctrl+Enter. - Run entire script: Press
Ctrl+Shift+Enteror click the "Source" button. - From terminal: Run
Rscript my_script.Rfrom the command line.
# Run an R script from the command line Rscript my_script.R # Run with arguments Rscript my_script.R arg1 arg2 # Start interactive R session R
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX