apexcharter/R/apex.R

145 lines
3.8 KiB
R
Raw Normal View History

2019-02-14 15:50:58 +01:00
#' Quick Apex Chart
#'
#' @param data Default dataset to use for chart. If not already a \code{data.frame}, it will be coerced to with \code{as.data.frame}.
#' @param mapping Default list of aesthetic mappings to use for chart
#' @param type Specify the chart type. Available Options: \code{"column"}, \code{"bar"}, \code{"line"},
2019-02-14 18:33:28 +01:00
#' \code{"area"}, \code{"spline"}, \code{"pie"}, \code{"donut"}, \code{"radialBar"}, \code{"radar"}, \code{"scatter"}, \code{"bubble"}, \code{"heatmap"}.
2019-02-14 15:50:58 +01:00
#' @param ... Other arguments passed on to methods. Not currently used.
2019-02-15 23:33:40 +01:00
#' @param auto_update In Shiny application, update existing chart rather than generating new one.
2019-02-14 15:50:58 +01:00
#' @param width A numeric input in pixels.
#' @param height A numeric input in pixels.
#' @param elementId Use an explicit element ID for the widget.
#'
#' @export
#'
#' @importFrom rlang eval_tidy as_name
#' @importFrom utils modifyList
#'
2019-02-15 23:33:40 +01:00
apex <- function(data, mapping, type = "column", ..., auto_update = TRUE, width = NULL, height = NULL, elementId = NULL) {
2019-02-14 18:33:28 +01:00
type <- match.arg(type, c("column", "bar", "line", "area", "spline", "area-spline",
"pie", "donut", "radialBar", "radar", "scatter", "bubble", "heatmap"))
2019-02-14 15:50:58 +01:00
data <- as.data.frame(data)
2019-02-14 16:37:40 +01:00
mapdata <- lapply(mapping, rlang::eval_tidy, data = data)
2019-02-14 18:33:28 +01:00
if (type %in% c("pie", "donut", "radialBar", "radar")) {
opts <- list(
chart = list(type = correct_type(type)),
series = list1(mapdata$y),
labels = list1(mapdata$x)
)
} else {
opts <- list(
chart = list(type = correct_type(type)),
series = make_series(mapdata, mapping, type)
)
}
2019-02-14 16:37:40 +01:00
opts <- modifyList(opts, choose_config(type, is_datetime(mapdata)))
2019-02-15 23:33:40 +01:00
apexchart(
ax_opts = opts, width = width, height = height,
elementId = elementId, auto_update = auto_update
)
2019-02-14 15:50:58 +01:00
}
# Construct series
2019-02-14 16:37:40 +01:00
make_series <- function(mapdata, mapping, type) {
2019-02-14 15:50:58 +01:00
mapdata <- as.data.frame(mapdata)
series_names <- "Series"
2019-02-14 18:33:28 +01:00
if (!is.null(mapping$y))
2019-02-14 15:50:58 +01:00
series_names <- rlang::as_name(mapping$y)
2019-02-14 18:33:28 +01:00
series <- list(list(
name = series_names,
data = parse_df(mapdata, add_names = names(mapping))
))
2019-02-14 15:50:58 +01:00
if ("fill" %in% names(mapping)) {
series <- lapply(
X = unique(mapdata$fill),
FUN = function(x) {
list(
name = x,
data = parse_df(mapdata[mapdata$fill %in% x, ], add_names = names(mapping))
)
}
)
}
series
}
2019-02-14 16:37:40 +01:00
is_datetime <- function(mapdata) {
inherits(mapdata$x, what = c("Date", "POSIXt"))
}
2019-02-14 18:33:28 +01:00
list1 <- function(x) {
if (length(x) == 1) {
list(x)
} else {
x
}
}
2019-02-14 15:50:58 +01:00
# Change type of charts for helpers type
correct_type <- function(type) {
if (identical(type, "column")) {
"bar"
2019-02-14 16:37:40 +01:00
} else if (identical(type, "spline")) {
"line"
2019-02-14 15:50:58 +01:00
} else {
type
}
}
# Switch between auto configs according to type & mapping
2019-02-14 16:37:40 +01:00
choose_config <- function(type, datetime) {
2019-02-14 15:50:58 +01:00
switch(
type,
"bar" = config_bar(horizontal = TRUE),
"column" = config_bar(horizontal = FALSE),
2019-02-14 16:37:40 +01:00
"line" = config_line(datetime = datetime),
"area" = config_line(datetime = datetime),
"spline" = config_line(curve = "smooth", datetime = datetime),
2019-02-14 15:50:58 +01:00
list()
)
}
# Config for column & bar charts
config_bar <- function(horizontal = FALSE) {
config <- list(
plotOptions = list(
bar = list(
horizontal = horizontal
)
)
)
if (isTRUE(horizontal)) {
config <- c(config, list(
grid = list(
yaxis = list(lines = list(show = FALSE)),
xaxis = list(lines = list(show = TRUE))
)
))
}
config
}
2019-02-14 16:37:40 +01:00
# Config for line, spline, area, area-spline
config_line <- function(curve = "straight", datetime = FALSE) {
config <- list(
stroke = list(
curve = curve
)
)
if (isTRUE(datetime)) {
config <- c(config, list(
xaxis = list(type = "datetime")
))
}
config
}