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

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

问题描述

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

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

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

例如,如果我加载 testthat ,则

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传递给

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

或者,用 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之类的文件加载第三方软件包时,您可能会看到那些软件包中的功能被默认软件包(如 stats )中的那些软件包所掩盖,而不是相反,如果您加载了R的启动过程完成后的第三方软件包.

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)

将其转换为数据框,以便与dplyr轻松使用.

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