7

Read this post. And this one.

I would like to load packages in a oner and supress suppressPackageStartupMessages at the same time.

The answer in the first link uses lapply:

packages <- c("tidyverse", "qdap", "stringr", "stringi", "textstem", "foreach", "caret", "xgboost", "quanteda")
lapply(packages, require, character.only = T)

This returns an unsightly list to the console:

[[1]]
[1] TRUE

[[2]]
[1] TRUE

[[3]]
[1] TRUE

[[4]]
[1] TRUE

Plus, I would like to also get rid of those messages that are sent to the console on load e.g.

> library(tidyverse)
Loading tidyverse: ggplot2
Loading tidyverse: dplyr
Conflicts with tidy packages --------------------------------------------------------------------
accumulate(): purrr, foreach
filter():     dplyr, stats
lag():        dplyr, stats
when():       purrr, foreach

Is there a clever, short way to both load a vector of packages AND suppressPackageStartupMessages?

amonk
  • 1,769
  • 2
  • 18
  • 27
Doug Fir
  • 19,971
  • 47
  • 169
  • 299
  • Here you have a function in github that may help you: https://gist.github.com/stevenworthington/3178163 – R18 Oct 11 '17 at 09:43

4 Answers4

12

One option would be

pacman::p_load(packages)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    To do that would I have to load pacman first ```library(pacman)``` and then load all the other libraries using pacman? Obviously an extra line of code is no biggie, but the goal, if I'm being pedantic, is to neatly call all packages in a oner. – Doug Fir Oct 12 '17 at 09:08
  • 2
    @DougFir In my plain new console, I do `pacman::p_load(reshape2, stringr, overflow, data.table, dplyr, tidyr, purrr)` and it loads all these packages in one single step and no warnings – akrun Oct 12 '17 at 09:47
  • Ah right, because library:: is like calling the library one time right? – Doug Fir Oct 12 '17 at 12:06
  • 1
    This is nice too, if `pacman` is already installed or someone can install it. – Christopher Bottoms Nov 17 '21 at 16:06
11

You could do the following

suppressPackageStartupMessages({
    library(ggplot2)
    library(tidyr)
})
Luna
  • 368
  • 2
  • 7
5

I would go with:

packages <- c("tidyverse", "qdap", "stringr", "stringi", "textstem", "foreach", "caret", "xgboost", "quanteda")

zzz<-lapply(packages, function(xxx) suppressMessages(require(xxx, character.only = TRUE,quietly=TRUE,warn.conflicts = FALSE)))
amonk
  • 1,769
  • 2
  • 18
  • 27
5

To get rid of the package messages, you can use suppressPackageStartupMessages() or you can use the quietly = T option:

packages <- c("tidyverse","stringr")
lapply(packages, function(x)require(x, character.only = T, quietly = T))

To get rid of the list input as well, you can wrap the whole thing in the invisible() function:

packages <- c("tidyverse","stringr")
invisible(lapply(packages, function(x) require(x, character.only = T, quietly = T)))
Andrew Haynes
  • 2,612
  • 2
  • 20
  • 35
  • 1
    Thank you very much, never knew about invisible. Went with amonk's answer for no reason other than timing – Doug Fir Oct 11 '17 at 09:52