What Does "The Following Object Is Masked from 'Package:Xxx'" Mean

What does The following object is masked from 'package:xxx' mean?

The message means that both the packages have functions with the same names. In this particular case, the testthat and assertive packages contain five functions with the same name.

When two functions have the same name, which one gets called?

R will look through the search path to find functions, and will use the first one that it finds.

search()
## [1] ".GlobalEnv" "package:assertive" "package:testthat"
## [4] "tools:rstudio" "package:stats" "package:graphics"
## [7] "package:grDevices" "package:utils" "package:datasets"
## [10] "package:methods" "Autoloads" "package:base"

In this case, since assertive was loaded after testthat, it appears earlier in the search path, so the functions in that package will be used.

is_true
## function (x, .xname = get_name_in_parent(x))
## {
## x <- coerce_to(x, "logical", .xname)
## call_and_name(function(x) {
## ok <- x & !is.na(x)
## set_cause(ok, ifelse(is.na(x), "missing", "false"))
## }, x)
## }
<bytecode: 0x0000000004fc9f10>
<environment: namespace:assertive.base>

The functions in testthat are 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:

testthat::is_true
## function ()
## {
## function(x) expect_true(x)
## }
## <environment: namespace:testthat>

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 = FALSE to library.

library(testthat)
library(assertive, warn.conflicts = FALSE)
# No output this time

Alternatively, suppress the message with suppressPackageStartupMessages:

library(testthat)
suppressPackageStartupMessages(library(assertive))
# Also no output

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 ?Startup should solve most mysteries.

For example, the documentation there says:

Note that when the site and user profile files are sourced only the
base package is loaded, so objects in other packages need to be
referred to by e.g. utils::dump.frames or after explicitly loading the
package concerned.

Which implies that when 3rd party packages are loaded via files like .Rprofile you 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.

library(dplyr)
envs <- search() %>% setNames(., .)

For each environment, get the exported functions (and other variables).

fns <- lapply(envs, ls)

Turn this into a data frame, for easy use with dplyr.

fns_by_env <- data_frame(
env = rep.int(names(fns), lengths(fns)),
fn = unlist(fns)
)

Find cases where the object appears more than once.

fns_by_env %>% 
group_by(fn) %>%
tally() %>%
filter(n > 1) %>%
inner_join(fns_by_env)

To test this, try loading some packages with known conflicts (e.g., Hmisc, AnnotationDbi).

How do I prevent name conflict bugs?

The conflicted package throws an error with a helpful error message, whenever you try to use a variable with an ambiguous name.

library(conflicted)
library(Hmisc)
units
## Error: units found in 2 packages. You must indicate which one you want with ::
## * Hmisc::units
## * base::units

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

Its not an error message. Basically you can have functions that can have same names. Lets say I write a function named sum() in a package named summer. This function named sum() can add two numbers only at a time. But base R already has a package named sum, it is masked from base. Meaning whenever you call sum, now the function sum() in the package summer is called. Hope it helps.

The following object is masked from ‘package:ggplot2’:

cowplot package is masking ggplot2::ggsave to avoid confusion and throwing error of unrecognized object format when you try to to save cowplot object into e.g. pdf-file.
Please see example below:

library(ggplot2)
library(cowplot)

# make a plot
p <- qplot(1:10, 1:10)
# draw into the top-right corner of a larger plot area
p1 <- ggdraw() + draw_plot(p, .6, .6, .4, .4)
ggsave("check.pdf", p1)
# Saving 7.17 x 5.6 in image
# everything is OK

ggplot2::ggsave("check1,pdf", p1)
# Error: Unknown graphics device ''

In case facing problems using ggsave by default called of cowplot package you can call it as ggplot2::ggsave. For details please see What does "The following object is masked from 'package:xxx'" mean? in accordance to PoGibas's comment.

Meaning of objects being masked by the global environment

It means that you have objects (functions, usually) present in your global environment with the same name as (exported) things in your package. Type search() to see the order in which R resolves names.

The solution is to either,

  1. don't create objects with those names in your global environment
  2. rename the objects in your package to something that's less likely to create a conflict, or rethink your decision to export them, or
  3. remember that you will always have to refer to those objects as saber::teamStats.

Probably (2) is best, unless the circumstances that led to the message are truly unusual.

The following objects(s) are masked from 'package:datasets':

Nothing's wrong - it's loaded fine!

It's just saying you have two datasets with the same name defined in different packages. I imagine that they are the same, but if you wanted to use one or the other you could use:

effects::Titanic
datasets::Titanic

This will happen with functions as well as datasets. Same method to pick which function from which package.

Error message The following object is masked from... in R

First advice, don't use empty spaces in your variable names, using underscores is a good practice in R.

Second advice, if you just need to load this data once, use the datapasta package, you just have to select your data on Excell, press Ctrl + c, and then go to R console and type datapasta::df_paste().

Third advice, this kind of questions are more suited for https://community.rstudio.com

gdata - object is masked...

Have a look at

print(.packages())

If "gdata" is inside, then the package is successfully loaded. I suspect the package has been loaded, because you got the warning message for masking.

You don't need to worry too much about masking. You can always do gdata::nobs and stats::nobs to call nobs function in different packages. Similarly, you can always do gdata::object.size and utils::object.size to get the right function.

Mask output of `The following objects are masked from....:` after calling attach() function

You use attach without detach - every time you do it new call to attach masks objects attached before (they contain the same names). Either use detach or do not use attach at all.
Nice discussion and tips are here.



Related Topics



Leave a reply



Submit