First, clear out the workspace (after saving any R objects you will need later):
rm(list = ls())
Then, install the required packages:
The convention in this book will be to specify a simple install.packages("packagename") instruction to install the necessary packages, followed by a library("packagename") to load them into memory. Once the required packages are installed, you can either comment them out, or replace the line with conditional installation code:
try(require(dplyr) || install.packages("dplyr"))
But this syntax may not work on all R installations and GUIs, so we will keep ...