updated docs

This commit is contained in:
pvictor 2019-07-19 14:11:41 +02:00
parent 944e63e256
commit ece66ae816
11 changed files with 262 additions and 16 deletions

View File

@ -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

View File

@ -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,

View File

@ -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"

View File

@ -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')
}

View File

@ -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]
)
}

View File

@ -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)
}
}

View File

@ -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.

View File

@ -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
)
)
}

View File

@ -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")
)
)
)
)
}

View File

@ -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")
)
)
}

View File

@ -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")
```