diff --git a/DESCRIPTION b/DESCRIPTION index 32cd511..7090b75 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,5 +22,6 @@ Suggests: knitr, rmarkdown, dplyr, shiny, - manipulateWidget + manipulateWidget, + testthat VignetteBuilder: knitr diff --git a/R/add_flows.R b/R/add_flows.R index cf99253..8ac74c8 100644 --- a/R/add_flows.R +++ b/R/add_flows.R @@ -53,7 +53,7 @@ addFlows <- function(map, lng0, lat0, lng1, lat1, color = "blue", flow = 1, if (is.null(time)) time <- 1 if (is.null(layerId)) layerId <- sprintf("_flow (%s,%s) -> (%s,%s)", lng0, lat0, lng1, lat1) - options <- .makeOptions( + options <- .preprocessArgs( required = list(lng0 = lng0, lat0 = lat0, lng1 = lng1, lat1 = lat1, layerId = layerId, time = time), optional = list(dir = dir, color = color, value = flow, maxValue = maxFlow, minThickness = minThickness, maxThickness = maxThickness, @@ -84,7 +84,7 @@ updateFlows <- function(map, layerId, color = NULL, flow = NULL, opacity = NULL, minThickness = 1, maxThickness = 20) { if (is.null(time)) time <- 1 - options <- .makeOptions( + options <- .preprocessArgs( required = list(layerId = layerId, time = time), optional = list(dir = dir, color = color, value = flow, maxValue = maxFlow, minThickness = minThickness, maxThickness = maxThickness, diff --git a/R/add_minicharts.R b/R/add_minicharts.R index bc5443a..a5743c5 100644 --- a/R/add_minicharts.R +++ b/R/add_minicharts.R @@ -146,7 +146,7 @@ addMinicharts <- function(map, lng, lat, chartdata = 1, time = NULL, maxValues = labels <- "none" } - options <- .makeOptions( + options <- .preprocessArgs( required = list(lng = lng, lat = lat, layerId = layerId, time = time), optional = list(type = type, width = width, height = height, opacity = opacity, labels = labels, @@ -208,7 +208,7 @@ updateMinicharts <- function(map, layerId, chartdata = NULL, time = NULL, maxVal } } - options <- .makeOptions( + options <- .preprocessArgs( required = list(layerId = layerId, time = time), optional = list(type = type, width = width, height = height, opacity = opacity, labels = labels, diff --git a/R/make_options.R b/R/preprocess_args.R similarity index 53% rename from R/make_options.R rename to R/preprocess_args.R index d448dc6..4afa781 100644 --- a/R/make_options.R +++ b/R/preprocess_args.R @@ -1,6 +1,9 @@ # Copyright © 2016 RTE Réseau de transport d’électricité -#' Private function that prepare R arguments to be sent to javascript functions. +#' Private function that prepare arguments for other functions. It transforms +#' them in a table with one column per argument. In order to improve memory +#' management, optional arguments with a single value are not added to the table +#' but there value is stored in a specific list. #' #' @param required #' Named list of required parameters @@ -8,11 +11,13 @@ #' Named list of optional parameters #' #' @return -#' A data.frame where each column represent one parameter +#' A list with two elements: +#' - options: data.frame with required args and variing optional args +#' - staticOptions: a list with single value args. #' #' @noRd #' -.makeOptions <- function(required, optional) { +.preprocessArgs <- function(required, optional) { options <- do.call(data.frame, required) staticOptions <- list() for (o in names(optional)) { diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..acc36c6 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(leaflet.minicharts) + +test_check("leaflet.minicharts") diff --git a/tests/testthat/test-preprocess_args.R b/tests/testthat/test-preprocess_args.R new file mode 100644 index 0000000..3280e37 --- /dev/null +++ b/tests/testthat/test-preprocess_args.R @@ -0,0 +1,40 @@ +context(".preprocessArgs") + +describe(".preprocessArgs", { + it ("creates a data.frame with one column per argument", { + opts <- .preprocessArgs(list(a = 1:3, b = 4:6), list(c = 7:9, d = 10:12)) + expect_is(opts$options, "data.frame") + expect_equal(dim(opts$options), c(3, 4)) + expect_equal(names(opts$options), c("a", "b", "c", "d")) + }) + + it ("separates static optional arguments", { + opts <- .preprocessArgs(list(a = 1:3, b = 4:6), list(c = 7:9, d = 10)) + expect_is(opts$options, "data.frame") + expect_equal(dim(opts$options), c(3, 3)) + expect_equal(names(opts$options), c("a", "b", "c")) + expect_is(opts$staticOptions, "list") + expect_equal(names(opts$staticOptions), "d") + }) + + it ("does not separate static required arguments", { + opts <- .preprocessArgs(list(a = 1:3, b = 4), list(c = 7:9, d = 10:12)) + expect_is(opts$options, "data.frame") + expect_equal(dim(opts$options), c(3, 4)) + expect_equal(names(opts$options), c("a", "b", "c", "d")) + expect_equal(length(opts$staticOptions), 0) + }) + + it ("does not accept NULL required arguments", { + expect_error(.preprocessArgs(list(a = 1:3, b = NULL), list(c = 7:9, d = 10:12))) + }) + + it ("accepts NULL optional required", { + opts <- .preprocessArgs(list(a = 1:3, b = 4:6), list(c = 7:9, d = NULL)) + expect_is(opts$options, "data.frame") + expect_equal(dim(opts$options), c(3, 3)) + expect_equal(names(opts$options), c("a", "b", "c")) + expect_equal(length(opts$staticOptions), 0) + }) + +})