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

Commit 470c3a7

Browse files
committed
Dynamically register knit_print.ggvis method
1 parent 5c02b28 commit 470c3a7

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ export(is.prop_constant)
257257
export(is.prop_reactive)
258258
export(is.prop_variable)
259259
export(is.scaled_value)
260-
export(knit_print.ggvis)
261260
export(layer_arcs)
262261
export(layer_bars)
263262
export(layer_boxplots)

R/print.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ view_dynamic <- function(x, plot_id = rand_id("plot_"), port = NULL,
106106
)
107107
}
108108

109+
110+
# Dynamically registered. See zzz.R.
109111
#' Knit print method for ggvis plots.
110112
#'
111113
#' @keywords internal
112-
#' @export
113114
knit_print.ggvis <- function(x, options = list(), inline = FALSE, ...) {
114115
# Set height and width from knitr chunk options
115116
knitr_opts <- list(

R/zzz.r

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,35 @@
77
"For questions and other discussion, please use ",
88
"https://groups.google.com/group/ggvis.")
99
}
10+
11+
.onLoad <- function(libname, pkgname) {
12+
# 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.
15+
register_s3_method("knitr", "knit_print", "ggvis")
16+
}
17+
18+
register_s3_method <- function(pkg, generic, class, fun = NULL) {
19+
stopifnot(is.character(pkg), length(pkg) == 1)
20+
envir <- asNamespace(pkg)
21+
22+
stopifnot(is.character(generic), length(generic) == 1)
23+
stopifnot(is.character(class), length(class) == 1)
24+
if (is.null(fun)) {
25+
fun <- get(paste0(generic, ".", class), envir = parent.frame())
26+
}
27+
stopifnot(is.function(fun))
28+
29+
30+
if (pkg %in% loadedNamespaces()) {
31+
registerS3method(generic, class, fun, envir = envir)
32+
}
33+
34+
# Always register hook in case package is later unloaded & reloaded
35+
setHook(
36+
packageEvent(pkg, "onLoad"),
37+
function(...) {
38+
registerS3method(generic, class, fun, envir = envir)
39+
}
40+
)
41+
}

0 commit comments

Comments
 (0)