added tests
This commit is contained in:
parent
e298eb1332
commit
31ed0a6fc3
4
R/apex.R
4
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) {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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))
|
||||
})
|
||||
|
||||
}
|
||||
|
|
|
@ -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"))
|
||||
})
|
||||
|
||||
|
||||
|
|
@ -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")
|
||||
})
|
||||
|
||||
|
|
|
@ -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))
|
||||
})
|
Loading…
Reference in New Issue