File: zzz.R

package info (click to toggle)
r-cran-webshot 0.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 600 kB
  • sloc: javascript: 5,565; makefile: 5
file content (31 lines) | stat: -rw-r--r-- 1,004 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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))
    }
  )
}


.onLoad <- function(...) {
  # webshot provides methods for knitr::knit_print, but knitr isn't a Depends
  # or Imports of htmltools, only an Enhances. This code snippet manually
  # registers our method(s) with S3 once both webshot and knitr are loaded.
  register_s3_method("knitr", "knit_print", "webshot")
}