Installing R and RStudio: the environment

Install R and an IDE, use projects to keep working directories honest, and learn the handful of commands that answer most questions about a session.

Install and orient yourself

# macOS with Homebrew
brew install --cask r rstudio

# Debian and Ubuntu
sudo apt install r-base r-base-dev

# then, in R
R.version.string
.libPaths()                # where packages are found, in order
PaneWhat it does
SourceScripts, R Markdown and notebooks you edit and save
ConsoleCommands run immediately, one expression at a time
EnvironmentObjects in memory, and the history of what you typed
Files / Plots / Packages / HelpThe four tabs you will use constantly
GitVersion control, visible only when a project has a repository

R itself is a command-line interpreter. RStudio is an IDE built on top of it, and every window you click ultimately runs an expression in the console. Reading the console output is how you learn what the buttons do.

Projects and the working directory

getwd()            # the working directory: the source of half of all confusion
setwd("C:/Users/me/analysis")   # avoid this in scripts

# a project instead: File > New Project, then paths are relative to the project root
library(here)
here::here()                    # always the project root, no matter where you run from
read.csv(here("data", "raw", "sales.csv"))
  • An RStudio project sets the working directory on open and remembers which files were open, so a session is reproducible by default.
  • here() finds the project root by looking for a marker file such as an .Rproj or .git directory.
  • Never commit a script containing setwd("C:/Users/yourname/..."). It works on exactly one machine.

Packages and session facts

install.packages("tidyverse")     # once per library
library(tidyverse)                # once per session

sessionInfo()                     # R version, platform, every loaded package
packageVersion("dplyr")
search()                          # attached packages and objects
ls("package:stats")               # what a package exports

?lm                               # help for a function
??regression                      # full-text search across help pages
💡
The assignment operator is <-, and = at top level also works but means something else inside a function call: it binds an argument. Use <- for assignment and keep = for arguments so the two never blur.

FAQ

Do I need RStudio, or can I use R alone?
Plain R is the interpreter plus a console. An IDE is worth it for the environment pane, help browser, project handling and R Markdown rendering. Positron and VS Code with the R extension are equally valid alternatives.
Why does read.csv fail with cannot open the connection?
The path is relative to the working directory, which moved when you launched R from a different folder. Check with getwd(), or use here() so the path is always resolved from the project root.

Vectors, factors and data frames Reading and writing data

Last refreshed 2026-09-18.