R/zzz.R

Defines functions register_s3_method .onLoad .onUnload

# nocov start
.onUnload <- function(libpath) {
  library.dynam.unload("haven", libpath)
}


# Adapted from https://github.com/tidyverse/hms/blob/master/R/zzz.R
.onLoad <- function(...) {
  register_s3_method("pillar", "pillar_shaft", "haven_labelled")

  invisible()
}

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

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

  if (pkg %in% loadedNamespaces()) {
    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))
    }
  )
}

# nocov end
tidyverse/haven documentation built on Dec. 4, 2023, 2:32 a.m.