आर

2015-06-04 7 views
16

This question में मास्किंग विधियों और विशेष रूप से this answer में निम्नलिखित प्रश्न सामने आए: मैं आर में तरीकों के मास्किंग के बारे में चेतावनी कैसे प्राप्त कर सकता हूं?आर

यदि आप एक स्वच्छ आर सत्र में निम्न कोड चलाते हैं, तो आप देखेंगे कि dplyrlag के लिए डिफ़ॉल्ट विधि को बदलता है।

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 देते हैं, तो आप कई नकाबपोश वस्तुओं के लिए warnigns मिलता है, लेकिन 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 

एनबी: यह जब मैं require(dplyr) साथ dplyr लोड एक चेतावनी है करने के लिए पर्याप्त नहीं है। अगर मैं पैकेज को संलग्न किए बिना नामस्थान लोड करता हूं तो विधि भी अधिभारित हो जाती है (उदा। मैं dplyr::mutate पर कॉल करता हूं, या यहां तक ​​कि मैं dplyr फ़ंक्शन को कॉल करता हूं जो importFrom का उपयोग करके आयात किया गया था)।

+0

FWIW dplyr अगले संस्करण – hadley

उत्तर

8

अद्यतन अब github पर एक आर पैकेज है जो इन मुद्दों को हल करने का प्रयास करता है। यह अभी भी एक आदर्श समाधान से बहुत दूर है, लेकिन यह इस मुद्दे को हल करने की दिशा में कुछ रास्ता तय करता है। वर्तमान में इसमें 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 

यह केवल एक विचार है कि कोई मुखौटा एस 3 विधियों को कैसे ढूंढ सकता है। यह किसी भी तरह का एक आदर्श समाधान नहीं है, लेकिन मुझे लगता है कि जब तक कोई बेहतर विचार के साथ आता है तो कम से कम डीबगिंग में मदद मिलेगी।

#' 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 तरीके हैं। यह वास्तव में आपको नहीं बताता है, जो कि दूसरे को मास्क कर रहा है। यह सिर्फ संभावित समस्याओं को इंगित करता है।

+0

में उस विधि को ओवरराइड नहीं करता है, मैं वास्तव में अंडर नहीं करता हूं और यह गड़बड़ क्यों बनाई गई थी, विवादित वस्तुओं के नाम के कारण कई मुद्दे (बहुत अच्छे otrherwise) 'dplyr' पैकेज – Qbik