R/onAttach.R

Defines functions register_s3_method .onAttach

.onAttach <- function(libname, pkgname){
  register_s3_method("knitr", "knit_print", "data.frame")
  register_s3_method("knitr", "knit_print", "dml")
  register_s3_method("officer", "to_wml", "block_pour_docx")
}

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))
    }
  )
}
davidgohel/worded documentation built on Feb. 23, 2024, 1:09 a.m.