There are 2 options:
Either you can use the list of packages and compare it to the output from installed.packages(). After that, you can install the missing packages.
list.of.packages <- c("ggplot2", "Rcpp")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
OR
You can out your code in a package and make them as dependencies. So they will automatically be installed when you install the required package.