When I load a package, I get a message stating that:
"The following object is masked from 'package:xxx'
For example, if I load testthat then assertive, I get the following:
library(testthat)
library(assertive)
## Attaching package: ‘assertive’
##
## The following objects are masked from ‘package:testthat’:
##
## has_names, is_false, is_less_than, is_null, is_true
What does this message mean, and how do I prevent it?
The message means that both the packages have functions with the same names. In this particular case, the
testthatandassertivepackages contain five functions with the same name.When two functions have the same name, which one gets called?
R will look through the
searchpath to find functions, and will use the first one that it finds.In this case, since
assertivewas loaded aftertestthat, it appears earlier in the search path, so the functions in that package will be used.The functions in
testthatare not accessible in the usual way; that is, they have been masked.What if I want to use one of the masked functions?
You can explicitly provide a package name when you call a function, using the double colon operator,
::. For example:How do I suppress the message?
If you know about the function name clash, and don't want to see it again, you can suppress the message by passing
warn.conflicts = FALSEtolibrary.Alternatively, suppress the message with
suppressPackageStartupMessages:Impact of R's Startup Procedures on Function Masking
If you have altered some of R's startup configuration options (see
?Startup) you may experience different function masking behavior than you might expect. The precise order that things happen as laid out in?Startupshould solve most mysteries.For example, the documentation there says:
Which implies that when 3rd party packages are loaded via files like
.Rprofileyou may see functions from those packages masked by those in default packages like stats, rather than the reverse, if you loaded the 3rd party package after R's startup procedure is complete.How do I list all the masked functions?
First, get a character vector of all the environments on the search path. For convenience, we'll name each element of this vector with its own value.
For each environment, get the exported functions (and other variables).
Turn this into a data frame, for easy use with dplyr.
Find cases where the object appears more than once.
To test this, try loading some packages with known conflicts (e.g.,
Hmisc,AnnotationDbi).How do I prevent name conflict bugs?
The
conflictedpackage throws an error with a helpful error message, whenever you try to use a variable with an ambiguous name.