diff --git a/R/facets.R b/R/facets.R index ed09a81..7e012a4 100644 --- a/R/facets.R +++ b/R/facets.R @@ -33,8 +33,8 @@ build_facets <- function(chart) { } mapdata <- lapply(chart$x$mapping, eval_tidy, data = facet) new$x$ax_opts$series <- make_series(mapdata, chart$x$mapping, chart$x$type, chart$x$serie_name) - new$x$facet <- NULL new$height <- chart$x$facet$chart_height + new$x$facet <- NULL class(new) <- setdiff(class(new), "apex_facet") return(new) } @@ -68,13 +68,6 @@ build_grid <- function(content, nrow = NULL, ncol = NULL, col_gap = "0px", row_g } -#' @export -print.apex_facet <- function(x, ...) { - facets_charts <- build_facets(x) - TAG <- build_grid(facets_charts, nrow = x$x$facet$nrow, ncol = x$x$facet$ncol) - print(htmltools::browsable(TAG)) -} - #' Facet wrap for ApexCharts #' @@ -106,3 +99,31 @@ ax_facet_wrap <- function(ax, return(ax) } + + + + + + + + +# Print methods ----------------------------------------------------------- + +#' @export +print.apex_facet <- function(x, ...) { + facets_charts <- build_facets(x) + TAG <- build_grid(facets_charts, nrow = x$x$facet$nrow, ncol = x$x$facet$ncol) + print(htmltools::browsable(TAG)) +} + +knit_print.apex_facet <- function(x, ..., options = NULL) { + facets_charts <- build_facets(x) + TAG <- build_grid(facets_charts, nrow = x$x$facet$nrow, ncol = x$x$facet$ncol) + knitr::knit_print(htmltools::browsable(TAG), options = options, ...) +} + + + + + + diff --git a/R/onLoad.R b/R/onLoad.R index 62f8c9e..0352089 100644 --- a/R/onLoad.R +++ b/R/onLoad.R @@ -35,4 +35,5 @@ } } }, force = TRUE) + register_s3_method("knitr", "knit_print", "apex_facet") } diff --git a/R/utils.R b/R/utils.R index 497cd56..c8ce5c1 100644 --- a/R/utils.R +++ b/R/utils.R @@ -100,4 +100,29 @@ to_posix <- function(x) { +# From vignette('knit_print', package = 'knitr') +# and https://github.com/rstudio/htmltools/pull/108/files +register_s3_method <- function(pkg, generic, class, fun = NULL) { # nocov start + 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