gpt4 book ai didi

r - R中的掩蔽方法

转载 作者:行者123 更新时间:2023-12-03 14:19:50 26 4
gpt4 key购买 nike

This question尤其是 this answer提出了以下问题:如何获得有关 R 中方法屏蔽的警告?

如果您在干净的 R session 中运行以下代码,您会注意到加载 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

如果附上包裹 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 函数),该方法也会过载。

最佳答案

更新 现在 github 上有一个 R 包试图解决这些问题。它仍然远不是一个理想的解决方案,但它对解决问题有一定的帮助。目前有功能require , librarywarnS3Methods .

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方法。它实际上并没有告诉你,哪个掩盖了另一个。它只是指出潜在的问题。

关于r - R中的掩蔽方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30641471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com