How to stop printing messages while loading a package in R?

R ProgrammingServer Side ProgrammingProgramming

There are some annoying messages we get while loading a package in R and they are not useful until and unless we are not loading a new package. Since these messages looks like outputs they might be confusing especially when we are analysing string data. Therefore, we must get rid of them.

An example of message while loading BSDA package:>

> library(BSDA)

Loading required package − lattic

Attaching package − ‘BSDA’

The following object is masked from ‘package:datasets’ −

Orange

Here we have some messages while loading the package BSDA but we might not be interested in those messages if we are sure that package is installed in R and we are loading it correctly. Also, if a package is frequently used then these messages are annoying and we want to get rid of them. So, there is an easily solution to this, we can use suppressPackageStartupMessages while loading the package.

Example

> suppressPackageStartupMessages(library(BSDA))

When we use suppressPackageStartupMessages function, it disables all the messages we get while loading a package if the package is installed in R but it will not make an impact on errors or warnings.

Example

> suppressPackageStartupMessages(library(ROCR))
Error in library(ROCR) : there is no package called ‘ROCR’

Since I don’t have package ROCR installed in my R version, suppressPackageStartupMessages function throws an error.

> suppressPackageStartupMessages(library(data.table))
Warning message:
package ‘data.table’ was built under R version 3.6.2

Now, I do have data.table package installed but it was built under version 3.6.2 so it is showing a warning message because the version of R I am using for this is 3.6.1.

Let’s have a look at one more example. Suppose we want to load gdata package that comes with a lot of messages as shown below −

> library(gdata)
gdata: Unable to locate valid perl interpreter
gdata:
gdata: read.xls() will be unable to read Excel XLS and XLSX files
gdata: unless the 'perl=' argument is used to specify the location of a
gdata: valid perl intrpreter.
gdata:
gdata: (To avoid display of this message in the future, please ensure
gdata: perl is installed and available on the executable search path.)
gdata: Unable to load perl libaries needed by read.xls()
gdata: to support 'XLX' (Excel 97-2004) files.
gdata: Unable to load perl libaries needed by read.xls()
gdata: to support 'XLSX' (Excel 2007+) files.
gdata: Run the function 'installXLSXsupport()'
gdata: to automatically download and install the perl
gdata: libaries needed to support Excel XLS and XLSX formats.
Attaching package: ‘gdata’

The following object is masked from ‘package:purrr’ −

keep

The following objects are masked from ‘package:data.table’ −

first, last

The following object is masked from ‘package:stats’ −

nobs

The following object is masked from ‘package:utils’ −

object.size

The following object is masked from ‘package:base’ −

startsWith

Now we can easily ignore these message as follows −

> suppressPackageStartupMessages(library(gdata))

But if we have installed a new package then we must not ignore these messages because there might be some useful for us as we are new to that package.

raja
Published on 10-Aug-2020 15:29:11
Advertisements