Since I work on different PCs with same script, I always need to install packages, thus my package loading argument is always if(!require("ggplot2")){install.packages("ggplot2"); library(ggplot2)}
(ggplot2 as an example).
So I want to write a function as the title describes, my thought was
load_packages <- function(package){
if(!require(package)){install.packages(package); library(package)}
}
however despite it can install package through load_packages("ggplot2")
, it can't load as it says
Error in library(package) : there is no package called ‘package’