Reputation: 41
When I included janitor package with other packages, it did not load.
library(MASS, caret, stepPlr, janitor)
Error in library(MASS, caret, stepPlr, janitor) : object 'janitor' not found
When I ran that command separately with only janitor package, it got loaded into session; with no error
> library(janitor)
Warning message:
package ‘janitor’ was built under R version 3.3.3
Is there any limit with including x number of packages at a time? Or there is something wrong with my RStudio?
Upvotes: 2
Views: 6667
Reputation: 1765
The straight answer was indicated by @Technophobe01
sapply(c('MASS', 'caret', 'stepPlr', 'janitor'), require, character.only = TRUE)
Upvotes: 2
Reputation: 23004
The function p_load
from the pacman
package allows for listing multiple packages like this, and will install them if any are not already present:
library(pacman)
p_load(MASS, caret, stepPlr, janitor)
This is not only user-friendly, it also improves reproducibility for running the same script across multiple users or environments.
Upvotes: 5
Reputation: 8666
The library()
function isn't meant to load multiple libraries, a better approach is to create a list of packages, and use require()
to check if they are installed and if not install them. See example below:
requiredPackages <- c("MASS", "caret", "stepPlr", "janitor")
ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
ipak(requiredPackages)
Upvotes: 4