apexcharter/R/shiny-input.R

272 lines
7.3 KiB
R
Raw Normal View History

2020-03-03 20:05:03 +01:00
2020-03-04 11:55:23 +01:00
#' @title Retrieve click information in Shiny
#'
#' @description According to type of chart, different values are retrieved:
#' * **bar and column:** retrieve category (x-axis).
#' * **pie and donut:** retrieve label.
#' * **time-series:** retrieve x-axis value, you have to display markers
#' with size > 0 and set tooltip's options `intersect = TRUE` and `shared = FALSE`.
2020-03-17 12:22:31 +01:00
#' * **scatter:** retrieve XY coordinates.
2020-03-03 20:05:03 +01:00
#'
#' @template ax-default
2020-03-04 19:02:01 +01:00
#' @param inputId The id that will be used server-side for retrieving click.
2020-03-04 10:25:20 +01:00
#' @param multiple Allow multiple selection: \code{TRUE} or \code{FALSE} (default).
2020-03-04 12:04:47 +01:00
#' @param effect_type Type of effect for selected element, default is to use lightly darken color.
#' @param effect_value A larger value intensifies the select effect, accept value between 0 and 1.
2020-03-04 15:14:02 +01:00
#' @param session The Shiny session.
#'
2020-03-18 12:30:48 +01:00
#' @note If x-axis is of type datetime, value retrieved is of class \code{POSIXct}.
2020-03-03 20:05:03 +01:00
#'
#' @export
2020-03-04 15:14:02 +01:00
#'
#' @importFrom shiny getDefaultReactiveDomain
2020-03-03 20:05:03 +01:00
#'
#' @examples
2020-03-18 12:30:48 +01:00
#'
#' library(apexcharter)
#'
#' # Not in Shiny but you can still click on bars
#' data.frame(
#' month = month.abb,
#' value = sample(1:100, 12)
#' ) %>%
#' apex(aes(month, value)) %>%
#' set_input_click("month_click", multiple = TRUE)
#'
#'
#' # Interactive examples:
2020-03-17 12:22:31 +01:00
#' if (interactive()) {
#'
2020-11-02 11:30:44 +01:00
#' run_demo_input("click")
2020-03-17 12:22:31 +01:00
#'
#' }
2020-03-04 12:04:47 +01:00
set_input_click <- function(ax, inputId, multiple = FALSE,
effect_type = c("darken", "lighten", "none"),
2020-03-04 15:14:02 +01:00
effect_value = 0.35,
session = shiny::getDefaultReactiveDomain()) {
2020-03-04 12:04:47 +01:00
effect_type <- match.arg(effect_type)
2020-03-17 12:22:31 +01:00
if (is.null(session))
session <- list(ns = identity)
2020-03-04 12:04:47 +01:00
ax <- ax_states(ax, active = list(
allowMultipleDataPointsSelection = isTRUE(multiple),
filter = list(
type = effect_type,
value = effect_value
)
))
2020-03-04 22:52:13 +01:00
ax$x$shinyEvents$click <- list(
2020-03-04 15:14:02 +01:00
inputId = session$ns(inputId)
2020-03-03 20:05:03 +01:00
)
ax
}
2020-03-04 19:02:01 +01:00
#' Retrieve zoom information in Shiny
#'
#' @template ax-default
2020-03-18 12:30:48 +01:00
#' @param inputId The id that will be used server-side for retrieving zoom.
2020-03-04 19:02:01 +01:00
#' @param session The Shiny session.
2020-03-18 12:30:48 +01:00
#'
#' @note If x-axis is of type datetime, value retrieved is of class \code{POSIXct}.
2020-03-04 19:02:01 +01:00
#'
#' @export
2020-03-17 12:22:31 +01:00
#'
#' @importFrom shiny getDefaultReactiveDomain
2020-03-04 19:02:01 +01:00
#'
#' @examples
2020-03-17 12:22:31 +01:00
#' if (interactive()) {
#'
2020-11-02 11:30:44 +01:00
#' run_demo_input("zoom")
2020-03-17 12:22:31 +01:00
#'
#' }
2020-03-04 19:02:01 +01:00
set_input_zoom <- function(ax, inputId,
session = shiny::getDefaultReactiveDomain()) {
2020-03-17 12:22:31 +01:00
if (is.null(session))
session <- list(ns = identity)
2020-03-14 20:41:41 +01:00
ax$x$shinyEvents$zoomed <- list(
2020-03-04 19:02:01 +01:00
inputId = session$ns(inputId)
)
ax
}
2020-03-18 12:30:48 +01:00
#' Retrieve selection information in Shiny
#'
#' @template ax-default
2020-03-18 12:30:48 +01:00
#' @param inputId The id that will be used server-side for retrieving selection.
#' @param type Allow selection either on x-axis, y-axis or on both axis.
#' @param fill_color Background color of the selection rect which is drawn when user drags on the chart.
#' @param fill_opacity Opacity of background color of the selection rectangle.
#' @param stroke_width Border thickness of the selection rectangle.
#' @param stroke_dasharray Creates dashes in borders of selection rectangle.
#' Higher number creates more space between dashes in the border.
#' @param stroke_color Colors of selection border.
#' @param stroke_opacity Opacity of selection border.
#' @param xmin,xmax Start value of x-axis. Both \code{min} and \code{max} must be provided.
#' @param ymin,ymax Start value of y-axis. Both \code{min} and \code{max} must be provided.
#' @param session The Shiny session.
#'
#' @export
#'
#' @examples
#'
#' library(apexcharter)
#' data("economics", package = "ggplot2")
#'
#' # Not in Shiny so no events
#' # but you can still select an area on chart
#' apex(economics, aes(date, psavert), type = "line") %>%
#' set_input_selection("selection")
#'
#' # Default selection at start
#' apex(economics, aes(date, psavert), type = "line") %>%
#' set_input_selection(
#' inputId = "selection",
#' xmin = format_date("1980-01-01"),
#' xmax = format_date("1985-01-01")
#' )
set_input_selection <- function(ax,
inputId,
type = c("x", "xy", "y"),
fill_color = "#24292e",
fill_opacity = 0.1,
stroke_width = 1,
stroke_dasharray = 3,
stroke_color = "#24292e",
stroke_opacity = 0.4,
xmin = NULL,
xmax = NULL,
ymin = NULL,
ymax = NULL,
2020-03-18 12:30:48 +01:00
session = shiny::getDefaultReactiveDomain()) {
type <- match.arg(type)
if (is.null(session))
session <- list(ns = identity)
selection <- list(
enabled = TRUE, type = type,
fill = list(color = fill_color, opacity = fill_opacity),
stroke = list(
width = stroke_width, dashArray = stroke_dasharray,
color = stroke_color, opacity = stroke_opacity
)
)
if (!is.null(xmin) & !is.null(xmax)) {
selection$xaxis <- list(min = xmin, max = xmax)
}
if (!is.null(ymin) & !is.null(ymax)) {
selection$yaxis <- list(min = ymin, max = ymax)
}
ax <- ax_chart(
ax = ax,
selection = selection,
toolbar = list(autoSelected = "selection")
)
ax$x$shinyEvents$selection <- list(
inputId = session$ns(inputId),
type = type
)
ax
}
2020-03-17 12:22:31 +01:00
2020-07-27 18:55:31 +02:00
#' Retrieve chart's base64 dataURI.
#'
#' @template ax-default
2020-07-27 18:55:31 +02:00
#' @param inputId The id that will be used server-side for retrieving data.
#' @param session The Shiny session.
#'
#' @export
#'
#' @example examples/export-2.R
set_input_export <- function(ax, inputId,
2021-01-06 17:13:05 +01:00
session = shiny::getDefaultReactiveDomain()) { # nocov start
2020-07-27 18:55:31 +02:00
if (is.null(session))
session <- list(ns = identity)
ax$x$shinyEvents$export <- list(
inputId = session$ns(inputId)
)
ax
2021-01-06 17:13:05 +01:00
} # nocov end
2020-07-27 18:55:31 +02:00
# Demo --------------------------------------------------------------------
2021-01-06 17:13:05 +01:00
# nocov start
2020-07-27 18:55:31 +02:00
2020-03-17 12:22:31 +01:00
#' Run Shiny input events examples
#'
2020-03-18 12:30:48 +01:00
#' @param example Name of the example.
2020-03-17 12:22:31 +01:00
#'
#' @export
2020-04-15 19:32:15 +02:00
#'
#' @importFrom shiny shinyAppFile
2020-03-17 12:22:31 +01:00
#'
#' @examples
#' if (interactive()) {
#'
2020-07-27 09:26:28 +02:00
#' run_demo_input("click")
#' run_demo_input("zoom")
#' run_demo_input("selection")
2020-03-17 12:22:31 +01:00
#'
#' }
2021-01-06 17:13:05 +01:00
run_demo_input <- function(example = c("click", "zoom", "selection")) {
2020-03-17 12:22:31 +01:00
example <- match.arg(example)
shiny::shinyAppFile(
2021-05-11 10:18:08 +02:00
appFile = system.file("examples/input", example, "app.R", package = "apexcharter"),
2020-03-17 12:22:31 +01:00
options = list("display.mode" = "showcase")
)
}
2020-04-15 19:32:15 +02:00
#' Run Shiny synchronization example
#'
#' @export
#'
#' @importFrom shiny shinyAppFile
#'
#' @examples
#' if (interactive()) {
#'
2020-07-27 09:26:28 +02:00
#' run_demo_sync()
2020-04-15 19:32:15 +02:00
#'
#' }
2020-07-27 09:26:28 +02:00
run_demo_sync <- function() {
2020-04-15 19:32:15 +02:00
shiny::shinyAppFile(
2021-05-11 10:18:08 +02:00
appFile = system.file("examples/sync", "app.R", package = "apexcharter"),
2020-04-15 19:32:15 +02:00
options = list("display.mode" = "showcase")
)
}
2020-03-17 12:22:31 +01:00
2020-04-16 16:42:34 +02:00
#' Run Shiny spark boxes example
#'
#' @export
#'
#' @importFrom shiny shinyAppFile
#'
#' @examples
#' if (interactive()) {
#'
2020-11-02 11:30:44 +01:00
#' run_demo_sparkbox()
2020-04-16 16:42:34 +02:00
#'
#' }
2020-07-27 09:26:28 +02:00
run_demo_sparkbox <- function() {
2020-04-16 16:42:34 +02:00
shiny::shinyAppFile(
2021-05-11 10:18:08 +02:00
appFile = system.file("examples/spark", "app.R", package = "apexcharter"),
2020-04-16 16:42:34 +02:00
options = list("display.mode" = "showcase")
)
}
2021-01-06 17:13:05 +01:00
# nocov end