17

这个问题,特别是这个答案提出了以下问题:如何获得有关 R 中方法屏蔽的警告?

如果您在干净的 R 会话中运行以下代码,您会注意到加载dplyr更改了lag.

lag(1:3, 1)
## [1] 1 2 3
## attr(,"tsp")
## [1] 0 2 1
require(dplyr)
lag(1:3, 1)
## [1] NA  1  2

如果您附加 package dplyr,您会收到几个蒙面对象的警告,但不会收到有关lag被蒙面的默认方法的警告。原因是在调用 时,会调用包中lag的泛型函数stats

lag
## function (x, ...) 
## UseMethod("lag")
## <bytecode: 0x000000000c072188>
## <environment: namespace:stats>

只是告诉我methods(lag)有一种方法lag.default。我可以看到有两种方法使用getAnywhere

getAnywhere(lag.default)
## 2 differing objects matching ‘lag.default’ were found
## in the following places
## registered S3 method for lag from namespace dplyr
## namespace:dplyr
## namespace:stats
## Use [] to view one of them

但这要求我知道检查默认lag方法是否被dplyr. 有没有办法检查方法是否被屏蔽?也许有这样的功能:

checkMethodMasking(dplyr)
## The following methods are masked from 'package:dplyr':
##    lag.default

注意:当我加载dplyr. require(dplyr)如果我只加载名称空间而不附加包,该方法也会重载(例如,我调用dplyr::mutate,甚至我使用来自另一个包的函数,该dplyr函数调用使用 导入的函数importFrom)。

4

2 回答 2

8

更新现在github上有一个 R 包试图解决这些问题。它距离理想的解决方案还很远,但它对解决问题有一定的帮助。它目前具有功能requirelibrarywarnS3Methods

devtools::install_github("blasern/warnS3")
require(warnS3)

# Examples
require2(dplyr)
## Loading required package: dplyr
##
## Attaching package: ‘dplyr’
##
## The following object is masked from ‘package:stats’:
##  
##  filter
##
## The following objects are masked from ‘package:base’:
##   
##  intersect, setdiff, setequal, union
## 
## The following methods are masked by 'package:dplyr':
##  
##  'lag.default' from 'package:stats'

require2(roxygen2)
## Loading required package: roxygen2
## The following methods are masked by 'package:roxygen2':
##  
##  'escape.character' from 'package:dplyr'

warnS3Methods()
## The following methods are available in multiple packages: 
##  
##  'escape.character' in packages: dplyr, roxygen2
##  'lag.default' in packages: dplyr, stats

这只是一个关于如何找到掩码 S3 方法的想法。这绝不是一个完美的解决方案,但我想在有人提出更好的想法之前,它至少有助于调试。

#' Get all S3 methods from a package
#' 
#' Find all S3 methods from a package
#' 
#' @param pkg can be either the name of an installed package
#' or the path of a package
getPkgS3Methods <- function(pkg){
  if (basename(pkg) == pkg) pkg <- path.package(pkg)
  ns <- parseNamespaceFile(basename(pkg), 
                           dirname(pkg), 
                           mustExist = FALSE)
  if (length(ns$S3methods) == 0) return(NULL)
  df <- cbind.data.frame(basename(pkg), ns$S3methods)
  colnames(df) <- c("package", "method", "class", "other")
  df
}

#' Get masked S3 methods
#' 
#' Finds all S3 methods that are currently available that are
#' duplicated
getMaskedS3Methods <- function(){
  paths <- as.character(gtools::loadedPackages(silent = TRUE)[, "Path"])
  lst <- lapply(paths, getPkgS3Methods)
  all_methods <- do.call(rbind, lst)
  duplicates <- 
  duplicated(all_methods[, c("method", "class")]) |
    duplicated(all_methods[, c("method", "class")], fromLast = TRUE)
  res <- all_methods[duplicates, ]
  res[order(res$method, res$class, res$package), ]
}

从干净的工作区调用(具有上述功能,但未加载包),然后您可以观察以下内容:

getMaskedS3Methods()
## [1] package method  class   other  
## <0 rows> (or 0-length row.names)

require(dplyr)
getMaskedS3Methods()
## package method   class other
## 143   dplyr    lag default  <NA>
## 438   stats    lag default  <NA>

这只是告诉你这里有两种lag.default方法。它实际上并没有告诉你,哪个掩盖了另一个。它只是指出潜在的问题。

于 2015-06-04T16:23:52.747 回答
1

conflicted软件包(请参阅此处)现在为这个问题提供了一个潜在的解决方案。加载后,您会收到conflicted有关函数名称冲突的更明确的错误消息。您还可以使用conflict_prefer(详细信息here)来指定默认情况下要使用的包的功能以及应屏蔽的功能。

例如,这是我在尝试使用包中的函数时遇到的最近parallel错误nFactors

# Error: [conflicted] `parallel` found in 2 packages.
# Either pick the one you want with `::` 
# * nFactors::parallel
# * lattice::parallel
# Or declare a preference with `conflict_prefer()`
# * conflict_prefer("parallel", "nFactors")
# * conflict_prefer("parallel", "lattice")

然后我添加了

conflict_prefer("parallel", "nFactors") 

在脚本开头加载我的库的代码之后,以确保它parallel会调用nFactors::parallel我的代码。

于 2020-02-21T22:15:11.310 回答