I've visited the question on how to implement a script for checking whether the proper packages are loaded but none of them work when I've tried to modify the code to do other things e.g., load the listed package if it is already installed or take a list of packages rather than having to enter them one by one.
What I've tried:
# works for checking whether individual packages are installed and if not
# installing them (wrapped in a function to save space)
chinst <- function(x){if(x %in% rownames(installed.packages())==FALSE) {install.packages(x)}}
chinst("car")
# (NOT functioning) can't add an `library()` to the if function
chinst2 <- function(x){if(x %in% rownames(installed.packages())==FALSE) {if(x %in% rownames(available.packages())==FALSE) {paste(x,"is not a valid package name via CRAN")} else {install.packages(x)}} else {library(x)}}
# (NOT Functioning) check a list of packages rather than individual ones
pkgs <- list("MCMCpack", "BMA", "coda")
lapply(X = pkgs, FUN = chinst)