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

查看:36
本文介绍了“以下对象被 '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(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 的启动过程完成后加载了第 3 方包.

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天全站免登陆