【问题标题】:Why does R report a namespace conflict for identical functions?为什么 R 报告相同函数的命名空间冲突?
【发布时间】:2016-03-25 10:55:45
【问题描述】:

我正在尝试在 R 中创建包之间的命名空间冲突列表,但 conflicts() 函数有一些奇怪的行为,例如 p 包中的函数 shiny 和 htmltools(其中shiny 进口):

> require(shiny)
> ## print source from shiny namespace...
> shiny::p
function (...) 
tags$p(...)
<environment: namespace:htmltools>
> require(htmltools)
> ## print source from htmltools namespace...
> htmltools::p
function (...) 
tags$p(...)
<environment: namespace:htmltools>

所以p 的源是完全相同的,甚至来自这两个包的同一个命名空间(帮助文件也相同),但是如果你运行conflicts(detail = TRUE),这个函数被列为两个包之间的冲突。为什么这被列为冲突,有没有办法检测这种类型的“冲突”?是否与您可以直接从shiny 或htmltools 命名空间调用函数有关?

【问题讨论】:

标签: r namespaces shiny


【解决方案1】:

函数conflicts 只检查搜索路径上多个包中包含的名称。它不检查这些名称所表示的实际对象是否相同。

您可以使用objects()(也用于conflicts())或ls() 来查看包命名空间中对象的名称。例如:

head(objects("package:shiny"), 10)
 ## [1] "a"                "absolutePanel"    "actionButton"     "actionLink"       "addResourcePath" 
 ## [6] "animationOptions" "as.shiny.appobj"  "basicPage"        "bootstrapPage"    "br"

如果你跑了

which(objects("package:shiny") == "p")
which(objects("package:htmltools") == "p")

您将看到shiny 和htmltools 的命名空间都包含一个名为p 的对象。 conflicts 只是检查出现在search() 中多个命名空间中的所有名称,p 就是其中之一,如果您附加了shiny 和htmltools。

要弄清楚为什么shiny 包含一个实际上位于htmltools 命名空间中的函数,您可以查看shiny 源中的NAMESPACE 文件。它包含以下几行

export(p)
import(htmltools)

export 使函数对使用包的人可见。因此,您可以在使用library() 附加一个包后调用导出的函数。 import(htmltools) 确保htmltools 中的所有导出函数 都可以被shiny 中的函数使用。但是,它不会使运行 library(shiny) 的人可以使用来自 htmltools 的函数。

因为shiny 导入来自htmltools(包括p())的所有函数,然后导出p(),p()(来自@987654352 @) 在library(shiny) 之后可见。

您可以使用identical() 检查两个冲突的对象是否实际上相同。这将告诉您shiny 和htmltools 中的函数p() 是相同的:

library(shiny)
library(htmltools)
identical(htmltools::p, shiny::p)
## [1] TRUE

另一方面,dplyr 定义了一个函数 filter(),它与 stats 中的函数不同:

library(dplyr)
identical(dplyr::filter, stats::filter)
## [1] FALSE

最后,让我提出一种方法来检查某些冲突(即出现在几个附加包中的名称)是否是“真正的”冲突。 “真实”是指共享名称的对象不相同。因此,上面示例中的filter() 将是“真正的”冲突,而“p”则不是。对于“真正的”冲突,以下函数返回 TRUE,否则返回 false:

is_real_conflict <- function(conf_obj) {

  # get list of all conflicts
  conf_all <- conflicts(detail = TRUE)
  # get names of packages that contain an object with name conf_obj
  conf_pck <- names(conf_all)[sapply(conf_all, function(pck) conf_obj %in% pck)]
  # get the actual objects associated with these names
  objs <- lapply(conf_pck, function(ns) get(conf_obj, envir = as.environment(ns)))
  # compare each of the objects to the first one
  comp <- sapply(objs, identical, objs[[1]])
  # the following returns FALSE only if all the objects are the same
  return (!all(comp))
}

您可以使用以下方法一次性检查所有冲突的名称

sapply(conflicts(), is_real_conflict)

【讨论】:

  • 非常清晰彻底的回答!作为未来读者的额外细节,identical() 似乎将返回FALSE,即使函数差异在语法上没有意义。例如,如果您定义 F &lt;- function(x) return(x); G &lt;- function(y) return(y); Z &lt;- function(x) return( x ),identical(F,G) 和 identical(F,Z) 都将返回 FALSE。
  • 是的,这是真的。 identical() 不检查两个函数对于相同的输入是否会给出相同的结果。
猜你喜欢
  • 2020-04-23
  • 2016-07-19
  • 2017-09-20
  • 1970-01-01
  • 2010-09-20
  • 1970-01-01
  • 2011-07-01
  • 2013-01-26
相关资源
最近更新 更多