简体   繁体   English

删除 R 中的加载包消息

[英]Removing loading package message in R

I am using the following packages in my R script:我在 R 脚本中使用以下包:

library(uba)
library(performanceEstimation)
library(UBL)
library(DMwR)

These packages always issue the message: 'loading package xyz'.这些包总是发出消息:'loading package xyz'。 If I don't suppress the 'loading package' messages as I am doing below:如果我不抑制“加载包”消息,就像我在下面做的那样:

suppressPackageStartupMessages(library(operators))
suppressPackageStartupMessages(library(class))
suppressPackageStartupMessages(library(fields))
suppressPackageStartupMessages(library(spam))
suppressPackageStartupMessages(library(dotCall64))
suppressPackageStartupMessages(library(grid))
suppressPackageStartupMessages(library(DMwR))
suppressPackageStartupMessages(library(uba))
suppressPackageStartupMessages(library(UBL))
suppressPackageStartupMessages(library(MBA))
suppressPackageStartupMessages(library(gstat))
suppressPackageStartupMessages(library(automap))
suppressPackageStartupMessages(library(sp))
suppressPackageStartupMessages(library(randomForest))

library(uba)  # util used below
library(performanceEstimation)
library(UBL)
library(DMwR)

I get the following messages in my console :我在控制台中收到以下消息

Loading required package: operators

Attaching package: 'operators'

The following objects are masked from 'package:base':

    options, strrep

Loading required package: class
Loading required package: fields
Loading required package: spam
Loading required package: dotCall64
Loading required package: grid
Spam version 2.5-1 (2019-12-12) is loaded.
Type 'help( Spam)' or 'demo( spam)' for a short introduction 
and overview of this package.
Help for individual functions is also obtained by adding the
suffix '.spam' to the function name, e.g. 'help( chol.spam)'.

Attaching package: 'spam'

The following objects are masked from 'package:base':

    backsolve, forwardsolve

Loading required package: maps
See https://github.com/NCAR/Fields for
 an extensive vignette, other supplements and source code 
Loading required package: ROCR
Loading required package: gplots

Attaching package: 'gplots'

The following object is masked from 'package:stats':

    lowess

Loading required package: DMwR
Loading required package: lattice
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 

Attaching package: 'DMwR'

The following object is masked from 'package:fields':

    unscale


Attaching package: 'uba'

The following object is masked from 'package:base':

    subset

Loading required package: MBA
Loading required package: gstat
Loading required package: automap
Loading required package: sp
Loading required package: randomForest
randomForest 4.6-14
Type rfNews() to see new features/changes/bug fixes.

Attaching package: 'UBL'

The following objects are masked from 'package:uba':

    phi, phi.control

Is there a way to suppress messages in any other way ?有没有办法以任何其他方式抑制消息? I might be using these packages in many other R scripts and I don't want to do this each time.我可能在许多其他 R 脚本中使用这些包,我不想每次都这样做。

One way is to write a simple function, that takes in a vector of packages and suppresses the output in a way you want, as examplified below.一种方法是编写一个简单的函数,它接收一个包向量并以您想要的方式抑制输出,如下所示。

library_suppress <- function(packages, character.only = TRUE, ...){
    for(i in seq_along(packages))
        suppressPackageStartupMessages(
            library(packages[i], character.only = character.only, ...)
        )
}
packages <- c('operators', 'class', 'fields', 'spam', 'dotCall64', 'grid', 'MBA', 'gstat', 'automap', 'sp', 'randomForest')
library_suppress(packages)

Note this does not suppress warning messages.请注意,这不会抑制警告消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM