Lesson 2 / 25
Installing R, IDEs, Packages and Projects
Set up R with an IDE, install packages and keep projects reproducible.
A productive environment
Install R itself from CRAN, then an editor. RStudio (from Posit) has long been the standard IDE, with a console, script editor, environment viewer, plots pane and help; Positron, Posit's newer data-science IDE built on VS Code foundations, supports both R and Python; and VS Code with the R extension also works. Install packages from CRAN with install.packages("dplyr") (once per machine) and load them in each session with library(dplyr). The tidyverse meta-package installs and loads core packages together: ggplot2, dplyr, tidyr, readr, purrr, tibble, stringr, forcats and lubridate. Work in projects (an .Rproj file or folder) so file paths are relative to the project, and avoid setwd() with absolute paths that break on other machines. For reproducibility, renv records exact package versions in a lockfile (renv::init(), renv::snapshot(), renv::restore()), similar to lock files in other languages. Get help with ?mean, help(package = "dplyr") and the vignettes that many packages include.
Packages and a reproducible project setup
Install once, load every session, and lock versions with renv.
install.packages(c("tidyverse", "renv")) # once per machine
library(tidyverse) # every session
# inside an RStudio/Positron project folder:
renv::init() # create a project library and renv.lock
# ... install or update packages as you work ...
renv::snapshot() # record exact versions in renv.lock
# a colleague later runs:
renv::restore() # installs the same versions
?read_csv # help page
vignette("dplyr") # long-form guide
sessionInfo() # R and package versions for bug reportsA kitchen with labelled jars
install.packages buys ingredients for your pantry; library() takes the jar off the shelf for today's cooking. renv writes down exactly which brand and batch you used, so someone else can cook the same dish.
Quick check: What is the difference between install.packages() and library()?
- There is none
- library downloads; install.packages loads
- Both must be run before every line
- install.packages downloads a package once; library loads it into the current session
Answer
install.packages downloads a package once; library loads it into the current session — Installation happens once per machine; loading happens in each R session.