Skip to content
This repository was archived by the owner on Feb 10, 2026. It is now read-only.

Commit d56e80c

Browse files
committed
Fix S3 method registration
Previously, the register_s3_method function called asNamespace(pkg), which tries to load the package, but the purpose of the function is to not load the package immediately.
1 parent e95fb1f commit d56e80c

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

R/zzz.r

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,31 @@
1010

1111
.onLoad <- function(libname, pkgname) {
1212
# ggvis provides methods for knitr::knit_print, but knitr isn't a Depends or
13-
# Imports of htmltools, only a Suggests. This code snippet manually
14-
# registers our method(s) with S3 once both ggvis and knitr are loaded.
13+
# Imports of ggvis, only a Suggests. This code snippet manually registers
14+
# our method(s) with S3 once both ggvis and knitr are loaded.
1515
register_s3_method("knitr", "knit_print", "ggvis")
1616
}
1717

1818
register_s3_method <- function(pkg, generic, class, fun = NULL) {
1919
stopifnot(is.character(pkg), length(pkg) == 1)
20-
envir <- asNamespace(pkg)
21-
2220
stopifnot(is.character(generic), length(generic) == 1)
2321
stopifnot(is.character(class), length(class) == 1)
22+
2423
if (is.null(fun)) {
2524
fun <- get(paste0(generic, ".", class), envir = parent.frame())
25+
} else {
26+
stopifnot(is.function(fun))
2627
}
27-
stopifnot(is.function(fun))
28-
2928

3029
if (pkg %in% loadedNamespaces()) {
31-
registerS3method(generic, class, fun, envir = envir)
30+
registerS3method(generic, class, fun, envir = asNamespace(pkg))
3231
}
3332

3433
# Always register hook in case package is later unloaded & reloaded
3534
setHook(
3635
packageEvent(pkg, "onLoad"),
3736
function(...) {
38-
registerS3method(generic, class, fun, envir = envir)
37+
registerS3method(generic, class, fun, envir = asNamespace(pkg))
3938
}
4039
)
4140
}

0 commit comments

Comments
 (0)