diff --git a/R/apex.R b/R/apex.R index 3fef68c..78a5115 100644 --- a/R/apex.R +++ b/R/apex.R @@ -33,7 +33,7 @@ apex <- function(data, mapping, type = "column", ..., auto_update = TRUE, width series = make_series(mapdata, mapping, type) ) } - opts <- modifyList(opts, choose_config(type, is_datetime(mapdata))) + opts <- modifyList(opts, choose_config(type, is_x_datetime(mapdata))) apexchart( ax_opts = opts, width = width, height = height, elementId = elementId, auto_update = auto_update @@ -66,7 +66,7 @@ make_series <- function(mapdata, mapping, type) { } -is_datetime <- function(mapdata) { +is_x_datetime <- function(mapdata) { inherits(mapdata$x, what = c("Date", "POSIXt")) } list1 <- function(x) { diff --git a/R/utils.R b/R/utils.R index f0713d4..43cbdce 100644 --- a/R/utils.R +++ b/R/utils.R @@ -29,10 +29,6 @@ formatNoSci <- function(x) { #' @noRd .ax_opt <- function(ax, name, ...) { - if(!any(class(ax) %in% c("apexcharter", "apexcharter_Proxy"))){ - stop("ax must be a apexcharter or a apexcharterProxy object") - } - if (is.null(ax$x$ax_opts[[name]])) { ax$x$ax_opts[[name]] <- list(...) } else { diff --git a/inst/examples-proxy/bar-auto/server.R b/inst/examples-proxy/bar-auto/server.R index 85eb013..a3966f6 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), auto_update = F) + apex(data = data_r(), type = "bar", mapping = aes(x = manufacturer, y = n, fill = year)) }) } diff --git a/tests/testthat/test-apex-utils.R b/tests/testthat/test-apex-utils.R new file mode 100644 index 0000000..14ec001 --- /dev/null +++ b/tests/testthat/test-apex-utils.R @@ -0,0 +1,45 @@ +context("test-apex-utils") + +test_that("is_x_datetime works", { + expect_true(is_x_datetime(list(x = Sys.Date()))) + expect_true(is_x_datetime(list(x = Sys.time()))) + expect_false(is_x_datetime(list(x = letters))) +}) + + +test_that("list1 works", { + expect_is(list1(1), "list") + expect_is(list1(1:2), "integer") + expect_length(list1(1:2), 2) +}) + + +test_that("correct_type works", { + expect_identical(correct_type("bar"), "bar") + expect_identical(correct_type("column"), "bar") + expect_identical(correct_type("line"), "line") + expect_identical(correct_type("spline"), "line") + expect_identical(correct_type("pie"), "pie") +}) + + +test_that("make_series works", { + serie <- make_series(iris, aes(x = Sepal.Length, y = Sepal.Width)) + expect_is(serie, "list") + expect_length(serie, 1) + expect_length(serie[[1]], 2) + expect_named(serie[[1]], c("name", "data")) +}) + +test_that("make_series works with group", { + mapping <- aes(x = Sepal.Length, y = Sepal.Width, fill = Species) + mapdata <- lapply(mapping, rlang::eval_tidy, data = iris) + serie <- make_series(mapdata, mapping) + expect_is(serie, "list") + expect_length(serie, 3) + expect_length(serie[[1]], 2) + expect_named(serie[[1]], c("name", "data")) +}) + + + diff --git a/tests/testthat/test-ax_opt.R b/tests/testthat/test-ax_opt.R index a4e5593..5ef1e39 100644 --- a/tests/testthat/test-ax_opt.R +++ b/tests/testthat/test-ax_opt.R @@ -40,9 +40,11 @@ test_that("ax_opt2 works", { ), class = c("list", "apexcharter")) new_opts <- .ax_opt2(opts, "series", list(data = 1:3)) + new_opts <- .ax_opt2(new_opts, "chart", list(type = "line")) 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) + expect_identical(new_opts$x$ax_opts$chart$type, "line") }) diff --git a/tests/testthat/test-example-readme.R b/tests/testthat/test-example-readme.R new file mode 100644 index 0000000..96755bd --- /dev/null +++ b/tests/testthat/test-example-readme.R @@ -0,0 +1,40 @@ +context("test-example-readme") + +test_that("readme exemple works", { + + n_manufac <- structure(list( + manufacturer = c("audi", "chevrolet", "dodge", + "ford", "honda", "hyundai", "jeep", "land rover", "lincoln", + "mercury", "nissan", "pontiac", "subaru", "toyota", "volkswagen" + ), + n = c(18L, 19L, 37L, 25L, 9L, 14L, 8L, 4L, 3L, 4L, 13L, 5L, 14L, 34L, 27L) + ), row.names = c(NA, -15L), class = c("tbl_df", "tbl", "data.frame")) + + ax <- apexchart() %>% + ax_chart(type = "bar") %>% + ax_plotOptions(bar = barOpts( + horizontal = FALSE, + endingShape = "flat", + columnWidth = "70%", + dataLabels = list( + position = "top" + )) + ) %>% + ax_grid( + show = TRUE, + position = "front", + borderColor = "#FFF" + ) %>% + ax_series(list( + name = "Count", + data = n_manufac$n + )) %>% + ax_colors("#112446") %>% + ax_xaxis(categories = n_manufac$manufacturer) %>% + ax_title(text = "Number of models") %>% + ax_subtitle(text = "Data from ggplot2") + + + expect_is(ax, "apexcharter") + expect_true(!is.null(ax$x$ax_opts)) +})