diff --git a/R/facets.R b/R/facets.R index 5387313..9c078b7 100644 --- a/R/facets.R +++ b/R/facets.R @@ -206,6 +206,52 @@ ax_facet_wrap <- function(ax, +# Shiny ------------------------------------------------------------------- + +#' @export +apexfacetOutput <- function(outputId) { + htmltools::tagList( + shiny::uiOutput(outputId = outputId), + htmlwidgets::getDependency("apexcharter", "apexcharter") + ) +} + +#' @export +#' +#' @importFrom shiny exprToFunction createRenderFunction createWebDependency +#' @importFrom htmltools renderTags resolveDependencies +renderApexfacet <- function(expr, env = parent.frame(), quoted = FALSE) { + func <- exprToFunction(expr, env, quoted) + createRenderFunction( + func = func, + transform = function(result, shinysession, name, ...) { + if (is.null(result) || length(result) == 0) + return(NULL) + if (!inherits(result, "apex_facet")) { + stop( + "renderApexfacet: 'expr' must return an apexcharter facets.", + call. = FALSE + ) + } + facets_charts <- build_facets(result) + TAG <- build_grid( + content = facets_charts, + nrow = result$x$facet$nrow, + ncol = result$x$facet$ncol + ) + rendered <- renderTags(TAG) + deps <- lapply( + X = resolveDependencies(rendered$dependencies), + FUN = createWebDependency + ) + list( + html = rendered$html, + deps = deps + ) + }, apexfacetOutput, list() + ) +} + diff --git a/examples/facet-shiny.R b/examples/facet-shiny.R new file mode 100644 index 0000000..695d8f2 --- /dev/null +++ b/examples/facet-shiny.R @@ -0,0 +1,34 @@ + +library(shiny) +library(htmltools) +library(apexcharter) + +data("unhcr_ts") +refugees <- unhcr_ts %>% + subset(population_type == "Refugees (incl. refugee-like situations)") %>% + transform(date = as.Date(paste0(year, "-01-01"))) + + +ui <- fluidPage( + + tags$h2("Apexcharts Facets Example"), + + apexfacetOutput("myfacet") + +) + +server <- function(input, output, session) { + + output$myfacet <- renderApexfacet({ + apex(refugees, aes(date, n), type = "line") %>% + ax_yaxis(tickAmount = 5) %>% + ax_facet_wrap(vars(continent_origin)) + }) + +} + +shinyApp(ui, server) + + + +