shiny render func

This commit is contained in:
pvictor 2020-12-08 14:49:16 +01:00
parent 19d25ee1fd
commit 88956c582b
2 changed files with 80 additions and 0 deletions

View File

@ -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()
)
}

34
examples/facet-shiny.R Normal file
View File

@ -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)