quick apex

This commit is contained in:
pvictor 2019-02-14 15:50:58 +01:00
parent 112c179839
commit 2b9bbba54d
8 changed files with 168 additions and 17 deletions

View File

@ -1,5 +1,5 @@
Package: apexcharter
Version: 0.0.1.910
Version: 0.0.1.920
Title: Create Interactive Chart with the JavaScript 'ApexCharts' Library
Description: Provides an 'htmlwidgets' interface to 'apexcharts.js'.
Authors@R: c(
@ -14,7 +14,8 @@ Depends: R (>= 2.10)
Imports:
htmlwidgets,
magrittr,
data.table
rlang,
ggplot2
RoxygenNote: 6.1.1
URL: https://github.com/dreamRs/apexcharter
BugReports: https://github.com/dreamRs/apexcharter/issues

View File

@ -1,6 +1,8 @@
# Generated by roxygen2: do not edit by hand
export("%>%")
export(aes)
export(apex)
export(apexchartOutput)
export(apexchartProxy)
export(apexcharter)
@ -34,11 +36,13 @@ export(parse_df)
export(pieOpts)
export(radialBarOpts)
export(renderApexchart)
importFrom(data.table,transpose)
importFrom(ggplot2,aes)
importFrom(htmlwidgets,createWidget)
importFrom(htmlwidgets,shinyRenderWidget)
importFrom(htmlwidgets,shinyWidgetOutput)
importFrom(htmlwidgets,sizingPolicy)
importFrom(magrittr,"%>%")
importFrom(rlang,as_name)
importFrom(rlang,eval_tidy)
importFrom(stats,setNames)
importFrom(utils,modifyList)

100
R/apex.R Normal file
View File

@ -0,0 +1,100 @@
#' 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"},
#' \code{"area"}, \code{"pie"}, \code{"donut"}, \code{"radialBar"}, \code{"scatter"}, \code{"bubble"}, \code{"heatmap"}.
#' @param ... Other arguments passed on to methods. Not currently used.
#' @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
#'
apex <- function(data, mapping, type = "column", ..., width = NULL, height = NULL, elementId = NULL) {
type <- match.arg(type, c("column", "bar", "line", "area", "pie", "donut", "radialBar", "scatter", "bubble", "heatmap"))
data <- as.data.frame(data)
opts <- list(
chart = list(type = correct_type(type)),
series = make_series(data, mapping, type)
)
opts <- modifyList(opts, choose_config(type, mapping))
apexcharter(ax_opts = opts, width = width, height = height, elementId = elementId)
}
# Construct series
make_series <- function(data, mapping, type) {
mapdata <- lapply(mapping, rlang::eval_tidy, data = data)
mapdata <- as.data.frame(mapdata)
series_names <- "Series"
if (identical(names(mapping), c("x", "y"))) {
series_names <- rlang::as_name(mapping$y)
series <- list(list(
name = series_names,
data = parse_df(mapdata, add_names = names(mapping))
))
}
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
}
# Change type of charts for helpers type
correct_type <- function(type) {
if (identical(type, "column")) {
"bar"
} else {
type
}
}
# Switch between auto configs according to type & mapping
choose_config <- function(type, mapping) {
switch(
type,
"bar" = config_bar(horizontal = TRUE),
"column" = config_bar(horizontal = FALSE),
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
}

View File

@ -22,4 +22,10 @@ NULL
#' @name %>%
#' @export
#' @rdname apexcharter-exports
NULL
NULL
#' @importFrom ggplot2 aes
#' @name aes
#' @export
#' @rdname apexcharter-exports
NULL

View File

@ -8,7 +8,6 @@
#' reuse \code{data} names or a character vector of new names.
#'
#' @export
#' @importFrom data.table transpose
#' @importFrom stats setNames
#'
parse_df <- function(data, add_names = FALSE) {
@ -17,25 +16,33 @@ parse_df <- function(data, add_names = FALSE) {
l <- lapply(
X = data[],
FUN = function(x) {
if (inherits(x, "Date")) {
if (inherits(x, "Date") & identical(add_names, FALSE)) {
as.numeric(x) * 86400000
} else if (inherits(x, "POSIXt")) {
} else if (inherits(x, "POSIXt") & identical(add_names, FALSE)) {
as.numeric(x)
} else if (inherits(x, "factor")) {
as.character(x)
} else {
x
if (!identical(add_names, FALSE)) {
formatNoSci(x)
} else {
x
}
}
}
)
ll <- data.table::transpose(l)
if (isTRUE(add_names)) {
ll <- lapply(ll, as.list)
ll <- lapply(ll, setNames, nm = names_)
}
if (is.character(add_names) & length(add_names) == length(names_)) {
ll <- lapply(ll, as.list)
ll <- lapply(ll, setNames, nm = add_names)
}
ll <- lapply(
X = seq_len(nrow(data)),
FUN = function(i) {
res <- lapply(l, `[[`, i)
if (identical(add_names, FALSE)) {
res <- unname(res)
}
if (is.character(add_names) & length(add_names) == length(names_)) {
res <- setNames(res, nm = add_names)
}
return(res)
}
)
return(ll)
}

View File

@ -10,6 +10,10 @@ dropNulls <- function(x) {
if (!is.null(x)) x else y
}
formatNoSci <- function(x) {
if (is.null(x)) return(NULL)
format(x, scientific = FALSE, digits = 15)
}
#' Utility function to create ApexChart parameters JSON

28
man/apex.Rd Normal file
View File

@ -0,0 +1,28 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/apex.R
\name{apex}
\alias{apex}
\title{Quick Apex Chart}
\usage{
apex(data, mapping, type = "column", ..., 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}.}
\item{mapping}{Default list of aesthetic mappings to use for chart}
\item{type}{Specify the chart type. Available Options: \code{"column"}, \code{"bar"}, \code{"line"},
\code{"area"}, \code{"pie"}, \code{"donut"}, \code{"radialBar"}, \code{"scatter"}, \code{"bubble"}, \code{"heatmap"}.}
\item{...}{Other arguments passed on to methods. Not currently used.}
\item{width}{A numeric input in pixels.}
\item{height}{A numeric input in pixels.}
\item{elementId}{Use an explicit element ID for the widget.}
}
\description{
Quick Apex Chart
}

View File

@ -3,6 +3,7 @@
\name{apexcharter-exports}
\alias{apexcharter-exports}
\alias{\%>\%}
\alias{aes}
\title{apexcharter exported operators and S3 methods}
\description{
The following functions are imported and then re-exported