Skip to content

S3 methods

satijalab edited this page Aug 17, 2018 · 1 revision

Most functions in Seurat are written using S3-style generics and methods, but the package is compatible with both the S3 and S4 object systems.

Hadley Wickham has a great blog post on the usage/trade-offs of S3 and S4. As he suggests, we prefer to use S3-style in general, as its more friendly, is easier to code, and provides tab-autocompletion functionality.

S3

Hadley also has a great introduction to using S3. If you plan to use these in Seurat, make sure to read the section (14.4) on Generics and methods, which highlights that S3 generics are required to have two required arguments: the argument that determines the class for dispatching and ... to pass other arguments to methods. Additionally, the S3 generic needs to call UseMethod to allow R to dispatch based on the object class.

Below is a very brief example of how to structure an S3 generic and method for Seurat.

#' Some function
#'
#' Some function does something
#'
#' @param object An object
#' @param ... Arguments passed to other methods
#'
#' @return Some return value
#'
#' @export SomeFunction
#' @rdname SomeFunction
#'
SomeFunction <- function(object, ...) {
  UseMethod(generic = 'SomeFunction', object = object)
}

#' @param seurat.param A Seurat-specific paramter
#'
#' @describeIn SomeFunction Do something on Seurat objects
#' @export
#' @method SomeFunction Seurat
#'
SomeFunction.Seurat <- function(object, seurat.parm, ...) {
  # Do something
}