diff --git a/DESCRIPTION b/DESCRIPTION index 504ee18..914d6b1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: apexcharter -Version: 0.0.4.960 +Version: 0.0.4.970 Title: Create Interactive Chart with the JavaScript 'ApexCharts' Library Description: Provides an 'htmlwidgets' interface to 'apexcharts.js'. Authors@R: c( @@ -21,7 +21,8 @@ Suggests: testthat, dplyr, knitr, - rmarkdown + rmarkdown, + shiny RoxygenNote: 6.1.1 URL: https://github.com/dreamRs/apexcharter BugReports: https://github.com/dreamRs/apexcharter/issues diff --git a/R/apex-options.R b/R/apex-options.R index b87e8a5..0d6f69f 100644 --- a/R/apex-options.R +++ b/R/apex-options.R @@ -79,7 +79,21 @@ events_opts <- function(click = NULL, #' @note See \url{https://apexcharts.com/docs/options/plotoptions/bar/}. #' #' @export -#' +#' +#' @examples +#' +#' library(dplyr) +#' data("mpg", package = "ggplot2") +#' +#' apex(count(mpg, manufacturer), aes(manufacturer, n)) %>% +#' ax_plotOptions( +#' bar = bar_opts( +#' endingShape = "rounded", +#' columnWidth = 100, +#' distributed = TRUE +#' ) +#' ) +#' bar_opts <- function(horizontal = NULL, endingShape = NULL, columnWidth = NULL, @@ -116,7 +130,32 @@ bar_opts <- function(horizontal = NULL, #' @note See \url{https://apexcharts.com/docs/options/plotoptions/heatmap/}. #' #' @export -#' +#' +#' @examples +#' +#' df <- expand.grid( +#' month = month.name, +#' person = c("Obi-Wan", "Luke", "Anakin", "Leia") +#' ) +#' df$value <- sample(0:1, nrow(df), TRUE) +#' +#' apex( +#' data = df, +#' mapping = aes(x = month, y = person, fill = value), +#' type = "heatmap" +#' ) %>% +#' ax_plotOptions( +#' heatmap = heatmap_opts( +#' enableShades = FALSE, +#' colorScale = list( +#' ranges = list( +#' list(from = 0, to = 0.5, color = "#FF0000"), +#' list(from = 0.5, to = 1, color = "#088A08") +#' ) +#' ) +#' ) +#' ) +#' heatmap_opts <- function(radius = NULL, enableShades = NULL, shadeIntensity = NULL, @@ -210,7 +249,7 @@ radialBar_opts <- function(size = NULL, #' Use these options in \code{\link{ax_plotOptions}}. #' #' @param size Numeric. Custom size of the pie which will override the default size calculations. -#' @param donut List with two fields \code{donutSize} (Donut / ring size in percentage relative to the total pie area.) +#' @param donut List with two fields \code{size} (Donut / ring size in percentage relative to the total pie area.) #' and \code{background} (The background color of the pie). #' @param customScale Numeric. Transform the scale of whole pie/donut overriding the default calculations. #' @param offsetX Numeric. Sets the left offset of the whole pie area. @@ -221,7 +260,18 @@ radialBar_opts <- function(size = NULL, #' @note See \url{https://apexcharts.com/docs/options/plotoptions/pie/}. #' #' @export -#' +#' +#' @examples +#' +#' library(dplyr) +#' data("mpg", package = "ggplot2") +#' +#' apex(count(mpg, cyl), aes(cyl, n), type = "donut") %>% +#' ax_plotOptions( +#' pie = pie_opts( +#' donut = list(size = "90%", background = "#BABABA") +#' ) +#' ) pie_opts <- function(size = NULL, donut = NULL, customScale = NULL, diff --git a/R/apex.R b/R/apex.R index ece75db..93d5ab8 100644 --- a/R/apex.R +++ b/R/apex.R @@ -57,6 +57,9 @@ apex <- function(data, mapping, type = "column", ..., auto_update = TRUE, width type <- match.arg(type, c("column", "bar", "line", "area", "spline", "area-spline", "pie", "donut", "radialBar", "radar", "scatter", "heatmap")) data <- as.data.frame(data) + if (identical(type, "heatmap")) { + mapping <- rename_aes_heatmap(mapping) + } mapdata <- lapply(mapping, rlang::eval_tidy, data = data) if (type %in% c("pie", "donut", "radialBar")) { opts <- list( @@ -117,6 +120,19 @@ is_grouped <- function(x) { any(c("colour", "fill", "group") %in% x) } +rename_aes_heatmap <- function(mapping) { + n_mapping <- names(mapping) + n_mapping[n_mapping == "y"] <- "group" + if ("fill" %in% n_mapping) { + n_mapping[n_mapping == "fill"] <- "y" + } + if ("colour" %in% n_mapping) { + n_mapping[n_mapping == "colour"] <- "y" + } + names(mapping) <- n_mapping + return(mapping) +} + rename_aes <- function(mapping) { if ("colour" %in% names(mapping)) { names(mapping)[names(mapping) == "colour"] <- "group" diff --git a/R/apexcharter.R b/R/apexcharter.R index adbca1d..87a2464 100644 --- a/R/apexcharter.R +++ b/R/apexcharter.R @@ -15,15 +15,39 @@ #' @examples #' #' library(apexcharter) -#' +#' +#' # Use raw API by passing a list of +#' # parameters to the function +#' #' apexchart(ax_opts = list( -#' chart = list(type = "bar"), +#' chart = list( +#' type = "bar" +#' ), #' series = list(list( #' name = "Example", #' data = sample(1:100, 5) #' )), -#' xaxis = list(categories = LETTERS[1:5]) +#' xaxis = list( +#' categories = LETTERS[1:5] +#' ) #' )) +#' +#' +#' # Or use apexchart() to initialize the chart +#' # before passing parameters +#' +#' apexchart() %>% +#' ax_chart(type = "bar") %>% +#' ax_series( +#' list( +#' name = "Example", +#' data = sample(1:100, 5) +#' ) +#' ) %>% +#' ax_xaxis( +#' categories = LETTERS[1:5] +#' ) +#' apexchart <- function(ax_opts = list(), auto_update = TRUE, width = NULL, height = NULL, elementId = NULL) { # forward options using x @@ -75,6 +99,44 @@ apexchart <- function(ax_opts = list(), auto_update = TRUE, width = NULL, height #' @export #' #' @importFrom htmlwidgets shinyWidgetOutput shinyRenderWidget +#' +#' @examples +#' +#' if (interactive()) { +#' library(shiny) +#' +#' ui <- fluidPage( +#' fluidRow( +#' column( +#' width = 8, offset = 2, +#' tags$h2("Apexchart in Shiny"), +#' actionButton("redraw", "Redraw chart"), +#' apexchartOutput("chart") +#' ) +#' ) +#' ) +#' +#' server <- function(input, output, session) { +#' +#' output$chart <- renderApexchart({ +#' input$redraw +#' apexchart() %>% +#' ax_chart(type = "bar") %>% +#' ax_series( +#' list( +#' name = "Example", +#' data = sample(1:100, 5) +#' ) +#' ) %>% +#' ax_xaxis( +#' categories = LETTERS[1:5] +#' ) +#' }) +#' +#' } +#' +#' shinyApp(ui, server) +#' } apexchartOutput <- function(outputId, width = '100%', height = '400px'){ htmlwidgets::shinyWidgetOutput(outputId, 'apexcharter', width, height, package = 'apexcharter') } diff --git a/man/apexchart.Rd b/man/apexchart.Rd index 0bfec7b..be53cd1 100644 --- a/man/apexchart.Rd +++ b/man/apexchart.Rd @@ -28,12 +28,36 @@ Create a apexcharts.js widget library(apexcharter) +# Use raw API by passing a list of +# parameters to the function + apexchart(ax_opts = list( - chart = list(type = "bar"), + chart = list( + type = "bar" + ), series = list(list( name = "Example", data = sample(1:100, 5) )), - xaxis = list(categories = LETTERS[1:5]) + xaxis = list( + categories = LETTERS[1:5] + ) )) + + +# Or use apexchart() to initialize the chart +# before passing parameters + +apexchart() \%>\% + ax_chart(type = "bar") \%>\% + ax_series( + list( + name = "Example", + data = sample(1:100, 5) + ) + ) \%>\% + ax_xaxis( + categories = LETTERS[1:5] + ) + } diff --git a/man/apexcharter-shiny.Rd b/man/apexcharter-shiny.Rd index 39ab4e6..e4f4bb5 100644 --- a/man/apexcharter-shiny.Rd +++ b/man/apexcharter-shiny.Rd @@ -28,3 +28,41 @@ is useful if you want to save an expression in a variable.} Output and render functions for using apexcharter within Shiny applications and interactive Rmd documents. } +\examples{ + +if (interactive()) { + library(shiny) + + ui <- fluidPage( + fluidRow( + column( + width = 8, offset = 2, + tags$h2("Apexchart in Shiny"), + actionButton("redraw", "Redraw chart"), + apexchartOutput("chart") + ) + ) + ) + + server <- function(input, output, session) { + + output$chart <- renderApexchart({ + input$redraw + apexchart() \%>\% + ax_chart(type = "bar") \%>\% + ax_series( + list( + name = "Example", + data = sample(1:100, 5) + ) + ) \%>\% + ax_xaxis( + categories = LETTERS[1:5] + ) + }) + + } + + shinyApp(ui, server) +} +} diff --git a/man/ax_yaxis2.Rd b/man/ax_yaxis2.Rd index 6667a1a..71eff85 100644 --- a/man/ax_yaxis2.Rd +++ b/man/ax_yaxis2.Rd @@ -9,7 +9,7 @@ ax_yaxis2(ax, ...) \arguments{ \item{ax}{A \code{apexcharts} \code{htmlwidget} object.} -\item{...}{See arguments from \code{\link{ax_yaxis}}} +\item{...}{See arguments from \code{\link{ax_yaxis}}.} } \value{ A \code{apexcharts} \code{htmlwidget} object. diff --git a/man/bar_opts.Rd b/man/bar_opts.Rd index 52447d8..2a6d772 100644 --- a/man/bar_opts.Rd +++ b/man/bar_opts.Rd @@ -31,3 +31,18 @@ Use these options in \code{\link{ax_plotOptions}}. \note{ See \url{https://apexcharts.com/docs/options/plotoptions/bar/}. } +\examples{ + +library(dplyr) +data("mpg", package = "ggplot2") + +apex(count(mpg, manufacturer), aes(manufacturer, n)) \%>\% + ax_plotOptions( + bar = bar_opts( + endingShape = "rounded", + columnWidth = 100, + distributed = TRUE + ) + ) + +} diff --git a/man/heatmap_opts.Rd b/man/heatmap_opts.Rd index 035c9df..50d7dce 100644 --- a/man/heatmap_opts.Rd +++ b/man/heatmap_opts.Rd @@ -24,3 +24,29 @@ Use these options in \code{\link{ax_plotOptions}}. \note{ See \url{https://apexcharts.com/docs/options/plotoptions/heatmap/}. } +\examples{ + +df <- expand.grid( + month = month.name, + person = c("Obi-Wan", "Luke", "Anakin", "Leia") +) +df$value <- sample(0:1, nrow(df), TRUE) + +apex( + data = df, + mapping = aes(x = month, y = person, fill = value), + type = "heatmap" +) \%>\% + ax_plotOptions( + heatmap = heatmap_opts( + enableShades = FALSE, + colorScale = list( + ranges = list( + list(from = 0, to = 0.5, color = "#FF0000"), + list(from = 0.5, to = 1, color = "#088A08") + ) + ) + ) + ) + +} diff --git a/man/pie_opts.Rd b/man/pie_opts.Rd index e69f1b6..9a55510 100644 --- a/man/pie_opts.Rd +++ b/man/pie_opts.Rd @@ -10,7 +10,7 @@ pie_opts(size = NULL, donut = NULL, customScale = NULL, \arguments{ \item{size}{Numeric. Custom size of the pie which will override the default size calculations.} -\item{donut}{List with two fields \code{donutSize} (Donut / ring size in percentage relative to the total pie area.) +\item{donut}{List with two fields \code{size} (Donut / ring size in percentage relative to the total pie area.) and \code{background} (The background color of the pie).} \item{customScale}{Numeric. Transform the scale of whole pie/donut overriding the default calculations.} @@ -29,3 +29,15 @@ Use these options in \code{\link{ax_plotOptions}}. \note{ See \url{https://apexcharts.com/docs/options/plotoptions/pie/}. } +\examples{ + +library(dplyr) +data("mpg", package = "ggplot2") + +apex(count(mpg, cyl), aes(cyl, n), type = "donut") \%>\% + ax_plotOptions( + pie = pie_opts( + donut = list(size = "90\%", background = "#BABABA") + ) + ) +} diff --git a/vignettes/starting-with-apexcharts.Rmd b/vignettes/starting-with-apexcharts.Rmd index e34464e..4981a88 100644 --- a/vignettes/starting-with-apexcharts.Rmd +++ b/vignettes/starting-with-apexcharts.Rmd @@ -200,9 +200,11 @@ txhousing2 <- txhousing %>% filter(city %in% head(unique(city)), year %in% c(2000, 2001)) %>% rename(val_med = median) -apex(data = txhousing2, - type = "heatmap", - mapping = aes(x = date, y = scales::rescale(val_med), group = city)) %>% +apex( + data = txhousing2, + type = "heatmap", + mapping = aes(x = date, y = city, fill = scales::rescale(val_med)) +) %>% ax_dataLabels(enabled = FALSE) %>% ax_colors("#008FFB") ```