R/zzz.R

Defines functions .onUnload .onLoad register_s3_method

register_s3_method <- function(pkg, generic, class, fun = NULL) {
  stopifnot(is.character(pkg), length(pkg) == 1L)
  stopifnot(is.character(generic), length(generic) == 1L)
  stopifnot(is.character(class), length(class) == 1L)

  if (is.null(fun)) {
    fun <- get(paste0(generic, ".", class), envir = parent.frame())
  } else {
    stopifnot(is.function(fun))
  }

  if (isNamespaceLoaded(pkg)) {
    registerS3method(generic, class, fun, envir = asNamespace(pkg))
  }

  # Always register hook in case package is later unloaded & reloaded
  setHook(
    packageEvent(pkg, "onLoad"),
    function(...) {
      registerS3method(generic, class, fun, envir = asNamespace(pkg))
    }
  )
}

.onLoad <- function(libname, pkgname) {
  if(getRversion() < "3.6.0") {
    register_s3_method("ggplot2", "autoplot", "zoo")
    register_s3_method("ggplot2", "fortify", "zoo")
    register_s3_method("ggplot2", "scale_type", "yearmon")
    register_s3_method("ggplot2", "scale_type", "yearqtr")
  }
  invisible()
}

.onUnload <- function(libpath) {
  library.dynam.unload("zoo", libpath)
}
parsifal9/test documentation built on Dec. 31, 2020, 1:14 a.m.