[Bioc-devel] SetMethod to dispatch on class in unattached package

Martin Morgan mtmorg@n@b|oc @end|ng |rom gm@||@com
Wed Jan 30 18:16:20 CET 2019

A simple approach is to suppress the message (I think this is pragmatic, rather than dumb __) about unknown class (along with DESCRIPTION Suggests: Seurat)

setGeneric("getGE", function(x) standardGeneric("getGE"))

    setMethod("getGE", "seurat", function(x) Seurat::GetAssayData(x))

One fun thing to try is use hooks for package load; I would say that this is rarely used so may have surprising issues... Note the need to specify the location of the generic `PkgA::getGE` and that the method is defined in the global environment, rather than in the package environment, so would not have access to non-exported functions. The DESCRIPTION file has Suggests: Seurat

Something along the lines of

setGeneric("getGE", function(x) standardGeneric("getGE"))

.onLoad <-
    if ("Seurat" %in% loadedNamespaces()) {
            getGE, "seurat",
            function(x) Seurat::GetAssayData(x)
    } else {
            packageEvent("Seurat", "onLoad"),
            function(...) {
                    PkgA::getGE, "seurat",
                    function(x) Seurat::GetAssayData(x),
                    where = .GlobalEnv


On 1/29/19, 9:27 PM, "Bioc-devel on behalf of Brendan Innes" <bioc-devel-bounces using r-project.org on behalf of brendan.innes using mail.utoronto.ca> wrote:

    Hi friendly Bioc gang! I'm struggling with what seems like a silly problem. I'm trying to write a simple wrapper S4 generic that accesses the data slot of various S4 objects (seurat and SingleCellExperiment objects). So the generic is:
    setGeneric("getGE",function(x) standardGeneric("getGE"))
    And the methods are:
              function(x) Seurat::GetAssayData(x))
              function(x) SingleCellExperiment::logcounts(x))
    Problem is that when I install the package, I get the warning
    > in method for �getGE� with signature �"seurat"�: no definition for class �seurat�
    This isn't surprising, since Seurat isn't imported, but I don't want the user to have to import it if their data is in a SingleCellExperiment object. The function still seems to work fine if I attach Seurat and load a seurat object, so I'm tempted to just suppress the error (but darned if I know how to do that either). Before I do something so rash, does anyone have a suggestion for a less dumb way to do this?
    Thanks so much!
    	[[alternative HTML version deleted]]

More information about the Bioc-devel mailing list