Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading multiple R packages with a single command

Multiple R packages may be loaded using the function p_load function from pacman R package with the following command:

pacman::p_load("ggplot2", "lme4")

However, I want to use the command like this

Packages <- c("ggplot2", "lme4")
pacman::p_load(Packages)

which does not work. Wonder how this can be achieved?

like image 414
MYaseen208 Avatar asked Jan 30 '23 20:01

MYaseen208


1 Answers

Set character.only on TRUE

Packages <- c("ggplot2", "lme4")
Packages %in% loadedNamespaces() # check if the packages are loaded
# [1] FALSE FALSE

pacman::p_load(Packages, character.only = TRUE)

Packages %in% loadedNamespaces()
# [1] TRUE TRUE

From ?p_load:

"character.only : logical. If TRUE then p_load will only accept a single input which is a character vector containing the names of packages to load."

like image 183
loki Avatar answered Feb 03 '23 05:02

loki