This question and in particular this answer brought up the following question: How can I get a warning about the masking of methods in R?
If you run the following code in a clean R session, you'll notice that loading dplyr
changes the default method for 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 you attach the package dplyr
, you get warnigns for several masked objects, but no warning about the default method for lag
being masked. The reason is that when calling lag
, the generic function from the stats
package is called.
lag
## function (x, ...)
## UseMethod("lag")
## <bytecode: 0x000000000c072188>
## <environment: namespace:stats>
And methods(lag)
just tells me that there is a method lag.default
. I can see that there are two methods using 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
But this requires that I know to check if the default lag
method was changed by dplyr
. Is there any way to check if methods were masked? Perhaps there is a function like this:
checkMethodMasking(dplyr)
## The following methods are masked from 'package:dplyr':
## lag.default
NB: It is not enough to have a warning when I load dplyr
with require(dplyr)
. The method also gets overloaded if I just load the namespace without attaching the package (e.g. I call dplyr::mutate
, or even I use a function from another package that calls a dplyr
function that was imported using importFrom
).