“以下对象被 'package:xxx' 屏蔽"是什么意思?意思? [英] What does "The following object is masked from 'package:xxx'" mean?

查看:33
本文介绍了“以下对象被 'package:xxx' 屏蔽"是什么意思?意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我加载一个包时,我收到一条消息:

When I load a package, I get a message stating that:

"The following object is masked from 'package:xxx'

例如,如果我加载 testthat 然后 assertive,我得到以下信息:

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?

推荐答案

该消息表示这两个包具有相同名称的函数.在这种特殊情况下,testthatassertive 包包含五个同名的函数.

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.

R 将查看 search 路径查找函数,并将使用它找到的第一个函数.

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"

在这种情况下,由于 assertive 是在 testthat 之后加载的,它出现在搜索路径的较早位置,因此将使用该包中的函数.

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>

testthat 中的函数无法以通常的方式访问;也就是说,它们被掩盖.

The functions in testthat are not accessible in the usual way; that is, they have been masked.

您可以在调用函数时使用双冒号运算符显式提供包名称,::.例如:

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>

如何隐藏消息?

如果您知道函数名称冲突,并且不想再次看到它,您可以通过将 warn.conflicts = FALSE 传递给 library.

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

或者,使用 suppressPackageStartupMessages 抑制消息:

Alternatively, suppress the message with suppressPackageStartupMessages:

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

R 的启动过程对函数屏蔽的影响

如果您更改了 R 的某些启动配置选项(请参阅 ?Startup),您可能会遇到与预期不同的函数屏蔽行为.?Startup 中规定的事情发生的确切顺序应该可以解决大多数谜团.

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.

例如,那里的文档说:

请注意,当站点和用户配置文件仅来自基础包已加载,因此其他包中的对象需要由例如引用utils::dump.frames 或在显式加载包裹相关.

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.

这意味着当通过 .Rprofile 之类的文件加载第 3 方包时,您可能会看到这些包中的函数被 stats 等默认包中的函数所掩盖,而不是相反,如果你在 R 的启动程序完成后加载了 3rd 方包.

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.

首先,获取搜索路径上所有环境的字符向量.为方便起见,我们将使用自己的值命名该向量的每个元素.

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)

将其转换为数据框,以便与 d​​plyr 一起使用.

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)

要测试这一点,请尝试加载一些已知冲突的包(例如,Hmisc, AnnotationDbi).

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

每当您尝试使用名称不明确的变量时,conflicted 包都会抛出错误并提供有用的错误消息.

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

这篇关于“以下对象被 'package:xxx' 屏蔽"是什么意思?意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆