proxy method v1
This commit is contained in:
parent
7567a477fd
commit
c65591a727
|
@ -3,6 +3,7 @@
|
|||
export("%>%")
|
||||
export(apexchart)
|
||||
export(apexchartOutput)
|
||||
export(apexchartProxy)
|
||||
export(ax_annotations)
|
||||
export(ax_chart)
|
||||
export(ax_colors)
|
||||
|
@ -13,6 +14,7 @@ export(ax_labels)
|
|||
export(ax_legend)
|
||||
export(ax_markers)
|
||||
export(ax_plotOptions)
|
||||
export(ax_proxy_series)
|
||||
export(ax_responsive)
|
||||
export(ax_series)
|
||||
export(ax_series2)
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
|
||||
#' @title Proxy for \code{apexchart}
|
||||
#'
|
||||
#' @description Allow to update a chart in Shiny application.
|
||||
#'
|
||||
#' @param shinyId single-element character vector indicating the output ID of the
|
||||
#' chart to modify (if invoked from a Shiny module, the namespace will be added
|
||||
#' automatically)
|
||||
#' @param session the Shiny session object to which the chart belongs; usually the
|
||||
#' default value will suffice
|
||||
#'
|
||||
#' @export
|
||||
#'
|
||||
apexchartProxy <- function(shinyId, session = shiny::getDefaultReactiveDomain()) {
|
||||
|
||||
if (is.null(session)) {
|
||||
stop("apexchartProxy must be called from the server function of a Shiny app")
|
||||
}
|
||||
|
||||
if (!is.null(session$ns) && nzchar(session$ns(NULL)) && substring(shinyId, 1, nchar(session$ns(""))) != session$ns("")) {
|
||||
shinyId <- session$ns(shinyId)
|
||||
}
|
||||
|
||||
structure(
|
||||
list(
|
||||
session = session,
|
||||
id = shinyId,
|
||||
x = list()
|
||||
),
|
||||
class = "apexchart_Proxy"
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
#' Call a proxy method
|
||||
#'
|
||||
#' @param proxy A \code{apexchartProxy} \code{htmlwidget} object.
|
||||
#' @param name Proxy method.
|
||||
#' @param ... Arguments passed to method.
|
||||
#'
|
||||
#' @return A \code{apexchartProxy} \code{htmlwidget} object.
|
||||
#' @noRd
|
||||
.ax_proxy <- function(proxy, name, ...) {
|
||||
if (!"apexchart_Proxy" %in% class(proxy))
|
||||
stop("This function must be used with a apexchartProxy object", call. = FALSE)
|
||||
proxy$session$sendCustomMessage(
|
||||
type = sprintf("update-apexchart-%s", name),
|
||||
message = list(id = proxy$id, data = list(...))
|
||||
)
|
||||
proxy
|
||||
}
|
||||
.ax_proxy2 <- function(proxy, name, l) {
|
||||
if (!"apexchart_Proxy" %in% class(proxy))
|
||||
stop("This function must be used with a apexchartProxy object", call. = FALSE)
|
||||
proxy$session$sendCustomMessage(
|
||||
type = sprintf("update-apexchart-%s", name),
|
||||
message = list(id = proxy$id, data = l)
|
||||
)
|
||||
proxy
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#' @title Proxy for updating series.
|
||||
#'
|
||||
#' @description Allows you to update the series array overriding the existing one.
|
||||
#'
|
||||
#' @param proxy A \code{apexchartProxy} \code{htmlwidget} object.
|
||||
#' @param newSeries The series array to override the existing one.
|
||||
#' @param animate Should the chart animate on re-rendering.
|
||||
#'
|
||||
#' @export
|
||||
#'
|
||||
#' @examples
|
||||
#' \dontrun{
|
||||
#'
|
||||
#' if (interactive()) {
|
||||
#'
|
||||
#'
|
||||
#'
|
||||
#' }
|
||||
#'
|
||||
#' }
|
||||
ax_proxy_series <- function(proxy, newSeries, animate = TRUE) {
|
||||
.ax_proxy2(
|
||||
proxy = proxy,
|
||||
name = "series",
|
||||
l = list(newSeries = list(newSeries), animate = animate)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -15,24 +15,48 @@
|
|||
library(apexcharter)
|
||||
library(highcharter) # data
|
||||
library(dplyr)
|
||||
|
||||
|
||||
# Data --------------------------------------------------------------------
|
||||
|
||||
data("vaccines")
|
||||
library(tidyr)
|
||||
|
||||
|
||||
|
||||
|
||||
# Mtcars heatmap ----------------------------------------------------------
|
||||
|
||||
# Heatmap -----------------------------------------------------------------
|
||||
|
||||
#O trying to recreate "The Impact of Vaccines" (http://jkunst.com/highcharter/showcase.html)
|
||||
mtcars_long <- mtcars %>%
|
||||
tibble::rownames_to_column(var = "model") %>%
|
||||
gather(variable, value, -model)
|
||||
|
||||
|
||||
apexchart() %>%
|
||||
ax_chart(type = "heatmap") %>%
|
||||
ax_dataLabels(enabled = FALSE) %>%
|
||||
ax_series2(lapply(
|
||||
X = unique(mtcars_long$model),
|
||||
FUN = function(x) {
|
||||
list(
|
||||
name = x,
|
||||
data = parse_df(mtcars_long[mtcars_long$model == x, c("variable", "value")])
|
||||
)
|
||||
}
|
||||
)) %>%
|
||||
ax_xaxis(type = "category", categories = unique(mtcars_long$variable))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Large Heatmap -----------------------------------------------------------
|
||||
|
||||
# pretty slow
|
||||
|
||||
# trying to recreate "The Impact of Vaccines" (http://jkunst.com/highcharter/showcase.html)
|
||||
|
||||
data("vaccines", package = "highcharter")
|
||||
|
||||
apexchart() %>%
|
||||
ax_chart(type = "heatmap", animations = list(enabled = FALSE)) %>%
|
||||
ax_dataLabels(enabled = FALSE) %>%
|
||||
ax_series2(lapply(
|
||||
X = unique(vaccines$state),
|
||||
FUN = function(x) {
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
|
||||
# ------------------------------------------------------------------------
|
||||
#
|
||||
# Title : Update serie
|
||||
# By : Victor
|
||||
# Date : 2018-09-09
|
||||
#
|
||||
# ------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
library(shiny)
|
||||
library(apexcharter)
|
||||
|
||||
|
||||
|
||||
ui <- fluidPage(
|
||||
tags$h2("Radomly update serie"),
|
||||
apexchartOutput(outputId = "graph", width = "600px"),
|
||||
actionButton(inputId = "update", label = "Update data")
|
||||
)
|
||||
|
||||
server <- function(input, output, session) {
|
||||
|
||||
output$graph <- renderApexchart({
|
||||
apexchart() %>%
|
||||
ax_chart(type = "line") %>%
|
||||
ax_plotOptions(line = list(curve = "smooth")) %>%
|
||||
ax_dataLabels(enabled = FALSE) %>%
|
||||
ax_series(
|
||||
list(
|
||||
name = "rnorm",
|
||||
data = round(rnorm(20), 3)
|
||||
)
|
||||
)
|
||||
})
|
||||
|
||||
observeEvent(input$update, {
|
||||
apexchartProxy(shinyId = "graph") %>%
|
||||
ax_proxy_series(newSeries = list(data = round(rnorm(20), 3)))
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
shinyApp(ui, server)
|
||||
|
||||
|
|
@ -66,11 +66,11 @@ function get_widget(id){
|
|||
|
||||
if (HTMLWidgets.shinyMode) {
|
||||
// data = load
|
||||
Shiny.addCustomMessageHandler('update-apexcharts-series',
|
||||
Shiny.addCustomMessageHandler('update-apexchart-series',
|
||||
function(obj) {
|
||||
var chart = get_widget(data.id);
|
||||
var chart = get_widget(obj.id);
|
||||
if (typeof chart != 'undefined') {
|
||||
chart.updateSeries(obj.newSeries, obj.animate);
|
||||
chart.updateSeries(obj.data.newSeries, obj.data.animate);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/proxy.R
|
||||
\name{apexchartProxy}
|
||||
\alias{apexchartProxy}
|
||||
\title{Proxy for \code{apexchart}}
|
||||
\usage{
|
||||
apexchartProxy(shinyId, session = shiny::getDefaultReactiveDomain())
|
||||
}
|
||||
\arguments{
|
||||
\item{shinyId}{single-element character vector indicating the output ID of the
|
||||
chart to modify (if invoked from a Shiny module, the namespace will be added
|
||||
automatically)}
|
||||
|
||||
\item{session}{the Shiny session object to which the chart belongs; usually the
|
||||
default value will suffice}
|
||||
}
|
||||
\description{
|
||||
Allow to update a chart in Shiny application.
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/proxy.R
|
||||
\name{ax_proxy_series}
|
||||
\alias{ax_proxy_series}
|
||||
\title{Proxy for updating series.}
|
||||
\usage{
|
||||
ax_proxy_series(proxy, newSeries, animate = TRUE)
|
||||
}
|
||||
\arguments{
|
||||
\item{proxy}{A \code{apexchartProxy} \code{htmlwidget} object.}
|
||||
|
||||
\item{newSeries}{The series array to override the existing one.}
|
||||
|
||||
\item{animate}{Should the chart animate on re-rendering.}
|
||||
}
|
||||
\description{
|
||||
Allows you to update the series array overriding the existing one.
|
||||
}
|
||||
\examples{
|
||||
\dontrun{
|
||||
|
||||
if (interactive()) {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue