As a big proponent of the dplyr grammar, I have created a package that can be used to process a class often used in research (summarizedExperiment, or se) with similar syntax. (package name cleanse).
I have for instance defined a new filter
function called filter.SummarizedExperiment
that takes a se as its first argument and defined a dispatch function as filter <- function(se, axis, ...){UseMethod("filter")}
.
This works as expected, but after loading the cleanse package, the dplyr equivalent functions are masked, and filtering data frames does not work anymore.
Is there a way I can adjust my package so the original data frame version of dplyr can still be used and the dispatch will automatically use either the dplyr or cleanse version depending on whether its first argument is a data frame or a SummarizedExperiment? What would be the best approach to this?
PS1: I am aware of the solution of using explicit namespace, i.e. using dplyr::filter, but would like a solution where dispatch takes place automatically based on the class.
PS2: dplyr is a dependency of cleanse, so all users will have dplyr installed.
dplyr already defines a generic for filter
so just define your own method. Here we return the string se
when invoking filter
on an se
object so replace the body appropriately in filter.se
:
library(dplyr)
filter.se <- function(x, ...) "se"
filter.default <- filter.ts <- stats::filter
dplyr's filter
clobbers base R's filter and the last line above, which is optional, fixes that but if you don't use it then you will have to use stats::filter
whenever you want to use base filter
.
Test it out with:
filter(BOD, Time < 5)
filter(1:5, 3)
filter(structure(1, class = "se"))