Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load multiple packages AND supress messages

Tags:

r

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?

like image 796
Doug Fir Avatar asked Oct 11 '17 09:10

Doug Fir


People also ask

How do I load multiple packages in R?

You can install multiple packages by passing a vector of package names to the function, for example, install. packages(c("dplyr", "stringr")) . That function will install the requested packages, along with any of their non-optional dependencies.

How do I suppress Library messages in R?

suppressPackageStartupMessages() method in R language can be used to disable messages displayed upon loading a package in R. This method is used to suppress package startup messages. The package should be pre-installed in R, otherwise, a warning is displayed upon function call.

How do I load all packages in R?

Once the package is installed, you must load the package and only after it has been loaded you can use all the functions and datasets it contains. To load a package, run library(name_of_package) (this time "" around the name of the package are optional, but can still be used if you wish).


1 Answers

One option would be

pacman::p_load(packages)
like image 183
akrun Avatar answered Oct 04 '22 01:10

akrun