dplyr - Masking methods in R -


this question , in particular this answer brought following question: how can warning masking of methods in r?

if run following code in clean r session, you'll notice loading dplyr changes default method 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 

if attach package dplyr, warnigns several masked objects, no warning default method lag being masked. reason when calling lag, generic function stats package called.

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

and methods(lag) tells me there method lag.default. can see there 2 methods using getanywhere:

getanywhere(lag.default) ## 2 differing objects matching ‘lag.default’ found ## in following places ## registered s3 method lag namespace dplyr ## namespace:dplyr ## namespace:stats ## use [] view 1 of them 

but requires know check if default lag method changed dplyr. there way check if methods masked? perhaps there function this:

checkmethodmasking(dplyr) ## following methods masked 'package:dplyr': ##    lag.default 

nb: not enough have warning when load dplyr require(dplyr). method gets overloaded if load namespace without attaching package (e.g. call dplyr::mutate, or use function package calls dplyr function imported using importfrom).

update there r package on github tries solve these issues. still far ideal solution, goes som way towards solving issue. has functions require, library , warns3methods.

devtools::install_github("blasern/warns3") require(warns3)  # examples require2(dplyr) ## loading required package: dplyr ## ## attaching package: ‘dplyr’ ## ## following object masked ‘package:stats’: ##   ##  filter ## ## following objects masked ‘package:base’: ##    ##  intersect, setdiff, setequal, union ##  ## following methods masked 'package:dplyr': ##   ##  'lag.default' 'package:stats'  require2(roxygen2) ## loading required package: roxygen2 ## following methods masked 'package:roxygen2': ##   ##  'escape.character' 'package:dplyr'  warns3methods() ## following methods available in multiple packages:  ##   ##  'escape.character' in packages: dplyr, roxygen2 ##  'lag.default' in packages: dplyr, stats 

this idea of how 1 can find masked s3 methods. no means perfect solution, guess until comes better idea @ least debuging.

#' s3 methods package #'  #' find s3 methods package #'  #' @param pkg can either name of installed package #' or path of 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 }  #' masked s3 methods #'  #' finds s3 methods available #' 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), ] } 

called clean workspace (with above functions, no packages loaded), can observe following:

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> 

that tells here 2 lag.default methods. not tell you, 1 masking other. points out potential problems.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -