更新现在github上有一个 R 包试图解决这些问题。它距离理想的解决方案还很远,但它对解决问题有一定的帮助。它目前具有功能require
, library
和warnS3Methods
。
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
方法。它实际上并没有告诉你,哪个掩盖了另一个。它只是指出潜在的问题。