From e298eb1332f5af77316edbeedbbc435f51a2c932 Mon Sep 17 00:00:00 2001 From: pvictor Date: Fri, 15 Feb 2019 23:33:40 +0100 Subject: [PATCH] auto update --- DESCRIPTION | 2 +- R/apex.R | 8 +++-- inst/examples-proxy/bar-auto/server.R | 2 +- inst/htmlwidgets/apexcharter.js | 8 ++++- man/apex.Rd | 6 ++-- tests/testthat/test-ax_opt.R | 48 +++++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 tests/testthat/test-ax_opt.R diff --git a/DESCRIPTION b/DESCRIPTION index a61fb3c..5625726 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: apexcharter -Version: 0.0.2.900 +Version: 0.0.2.910 Title: Create Interactive Chart with the JavaScript 'ApexCharts' Library Description: Provides an 'htmlwidgets' interface to 'apexcharts.js'. Authors@R: c( diff --git a/R/apex.R b/R/apex.R index 21c35aa..3fef68c 100644 --- a/R/apex.R +++ b/R/apex.R @@ -6,6 +6,7 @@ #' @param type Specify the chart type. Available Options: \code{"column"}, \code{"bar"}, \code{"line"}, #' \code{"area"}, \code{"spline"}, \code{"pie"}, \code{"donut"}, \code{"radialBar"}, \code{"radar"}, \code{"scatter"}, \code{"bubble"}, \code{"heatmap"}. #' @param ... Other arguments passed on to methods. Not currently used. +#' @param auto_update In Shiny application, update existing chart rather than generating new one. #' @param width A numeric input in pixels. #' @param height A numeric input in pixels. #' @param elementId Use an explicit element ID for the widget. @@ -15,7 +16,7 @@ #' @importFrom rlang eval_tidy as_name #' @importFrom utils modifyList #' -apex <- function(data, mapping, type = "column", ..., width = NULL, height = NULL, elementId = NULL) { +apex <- function(data, mapping, type = "column", ..., auto_update = TRUE, width = NULL, height = NULL, elementId = NULL) { type <- match.arg(type, c("column", "bar", "line", "area", "spline", "area-spline", "pie", "donut", "radialBar", "radar", "scatter", "bubble", "heatmap")) data <- as.data.frame(data) @@ -33,7 +34,10 @@ apex <- function(data, mapping, type = "column", ..., width = NULL, height = NUL ) } opts <- modifyList(opts, choose_config(type, is_datetime(mapdata))) - apexchart(ax_opts = opts, width = width, height = height, elementId = elementId) + apexchart( + ax_opts = opts, width = width, height = height, + elementId = elementId, auto_update = auto_update + ) } diff --git a/inst/examples-proxy/bar-auto/server.R b/inst/examples-proxy/bar-auto/server.R index a3966f6..85eb013 100644 --- a/inst/examples-proxy/bar-auto/server.R +++ b/inst/examples-proxy/bar-auto/server.R @@ -26,7 +26,7 @@ function(input, output, session) { output$chart <- renderApexchart({ - apex(data = data_r(), type = "bar", mapping = aes(x = manufacturer, y = n, fill = year)) + apex(data = data_r(), type = "bar", mapping = aes(x = manufacturer, y = n, fill = year), auto_update = F) }) } diff --git a/inst/htmlwidgets/apexcharter.js b/inst/htmlwidgets/apexcharter.js index d508e30..988d069 100644 --- a/inst/htmlwidgets/apexcharter.js +++ b/inst/htmlwidgets/apexcharter.js @@ -29,7 +29,13 @@ HTMLWidgets.widget({ apexchart = new ApexCharts(document.querySelector("#" + el.id), ax_opts); apexchart.render(); } else { - apexchart.updateSeries(ax_opts.series); + if (x.auto_update) { + apexchart.updateSeries(ax_opts.series); + } else { + apexchart.destroy(); + apexchart = new ApexCharts(document.querySelector("#" + el.id), ax_opts); + apexchart.render(); + } } diff --git a/man/apex.Rd b/man/apex.Rd index 3b0191e..1cd8d63 100644 --- a/man/apex.Rd +++ b/man/apex.Rd @@ -4,8 +4,8 @@ \alias{apex} \title{Quick Apex Chart} \usage{ -apex(data, mapping, type = "column", ..., width = NULL, - height = NULL, elementId = NULL) +apex(data, mapping, type = "column", ..., auto_update = TRUE, + width = NULL, height = NULL, elementId = NULL) } \arguments{ \item{data}{Default dataset to use for chart. If not already a \code{data.frame}, it will be coerced to with \code{as.data.frame}.} @@ -17,6 +17,8 @@ apex(data, mapping, type = "column", ..., width = NULL, \item{...}{Other arguments passed on to methods. Not currently used.} +\item{auto_update}{In Shiny application, update existing chart rather than generating new one.} + \item{width}{A numeric input in pixels.} \item{height}{A numeric input in pixels.} diff --git a/tests/testthat/test-ax_opt.R b/tests/testthat/test-ax_opt.R new file mode 100644 index 0000000..a4e5593 --- /dev/null +++ b/tests/testthat/test-ax_opt.R @@ -0,0 +1,48 @@ +context("test-ax_opt") + +test_that("ax_opt works", { + + opts <- structure(list( + x = list(ax_opts = list( + chart = list(type ="bar") + )) + ), class = c("list", "apexcharter")) + + new_opts <- .ax_opt(opts, "series", data = 1:3) + + expect_length(new_opts$x$ax_opts, 2) + expect_named(new_opts$x$ax_opts, c("chart", "series")) + expect_identical(new_opts$x$ax_opts$series$data, 1:3) +}) + + +test_that("ax_opt update existing parameter", { + + opts <- structure(list( + x = list(ax_opts = list( + chart = list(type ="bar") + )) + ), class = c("list", "apexcharter")) + + new_opts <- .ax_opt(opts, "chart", type = "line") + + expect_length(new_opts$x$ax_opts, 1) + expect_named(new_opts$x$ax_opts, c("chart")) + expect_identical(new_opts$x$ax_opts$chart$type, "line") +}) + +test_that("ax_opt2 works", { + + opts <- structure(list( + x = list(ax_opts = list( + chart = list(type ="bar") + )) + ), class = c("list", "apexcharter")) + + new_opts <- .ax_opt2(opts, "series", list(data = 1:3)) + + expect_length(new_opts$x$ax_opts, 2) + expect_named(new_opts$x$ax_opts, c("chart", "series")) + expect_identical(new_opts$x$ax_opts$series$data, 1:3) +}) +