加载多个包并抑制消息

时间:2017-10-11 09:13:59

标签: r

阅读this帖子。 并且this一个。

我想在oner中加载包并同时压缩suppressPackageStartupMessages

第一个链接中的答案使用了lapply:

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

这会向控制台返回一个难看的列表:

[[1]]
[1] TRUE

[[2]]
[1] TRUE

[[3]]
[1] TRUE

[[4]]
[1] TRUE

另外,我还想摆脱那些在加载时发送到控制台的消息,例如

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

是否有一种聪明,简短的方法来加载包矢量和suppressPackageStartupMessages?

4 个答案:

答案 0 :(得分:5)

一个选项是

pacman::p_load(packages)

答案 1 :(得分:3)

我会选择:

<!DOCTYPE html>

<html>

<head>
  <title>Home View</title>
  <link rel="stylesheet" type="text/css" href="../style/homestyle.css">
</head>

<body>
  <!-- Header -->
  <div class="header">
    <div class="wrapper">
      <div class="logo"></div>
      <div class="info"></div>
      <div class="clear"></div>
      <div class="menu"></div>
    </div>
  </div>
  <!--  -->

  <!-- NavBar -->
  <div class="header">
    <div class="wrapper">
      <div class="nav-bar"></div>
      <div class="clear"></div>
    </div>
  </div>
  <!--  -->

  <!-- Product Grid -->
  <div class="wrapper">
    <div class="def1">
      <div class="product1"></div>
    </div>
    <div class="def2">
      <div class="product2"></div>
    </div>
    <div class="def3">
      <div class="product3"></div>
    </div>
    <div class="def4">
      <div class="product4"></div>
    </div>
    <div class="clear"></div>
  </div>
  <!--  -->

  <!-- Bottom Part -->
  <div class="wrapper">
    <div class="content-area"></div>
  </div>

  <!-- Footer -->
  <div class="header">
    <div class="wrapper">
      <div class="footer-area"></div>
    </div>
  </div>
</body>

</html>

答案 2 :(得分:2)

要删除包邮件,您可以使用suppressPackageStartupMessages(),也可以使用quietly = T选项:

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

为了摆脱列表输入,你可以将整个事物包装在invisible()函数中:

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

答案 3 :(得分:0)

您可以执行以下操作

suppressPackageStartupMessages({
    library(ggplot2)
    library(tidyr)
})