Beginner

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.

Installation Links
# 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
Tip: Always install the latest version of R. You can check your current version by running 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:

  1. Source Editor (top-left)

    Where you write R scripts (.R files) and R Markdown documents (.Rmd). Supports syntax highlighting, auto-completion, and code folding.

  2. Console (bottom-left)

    The interactive R console where you can type and execute commands directly. Output appears here.

  3. Environment / History (top-right)

    Shows all variables, data frames, and objects in your current R session. The History tab stores previously executed commands.

  4. 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.

R
# 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.

R Markdown (.Rmd)
---
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) or Cmd+Enter (macOS).
  • Run selected code: Highlight code and press Ctrl+Enter.
  • Run entire script: Press Ctrl+Shift+Enter or click the "Source" button.
  • From terminal: Run Rscript my_script.R from the command line.
Bash
# 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
📚
Next up: Now that your environment is ready, we will dive into R variables, data types, and how R handles different kinds of data.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.