/
shiny-input.R
271 lines (244 loc) · 7.28 KB
/
shiny-input.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#' @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`.
#' * **scatter:** retrieve XY coordinates.
#'
#' @template ax-default
#' @param inputId The id that will be used server-side for retrieving click.
#' @param multiple Allow multiple selection: \code{TRUE} or \code{FALSE} (default).
#' @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.
#' @param session The Shiny session.
#'
#' @note If x-axis is of type datetime, value retrieved is of class \code{POSIXct}.
#'
#' @export
#'
#' @importFrom shiny getDefaultReactiveDomain
#'
#' @examples
#'
#' 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:
#' if (interactive()) {
#'
#' run_demo_input("click")
#'
#' }
set_input_click <- function(ax, inputId, multiple = FALSE,
effect_type = c("darken", "lighten", "none"),
effect_value = 0.35,
session = shiny::getDefaultReactiveDomain()) {
effect_type <- match.arg(effect_type)
if (is.null(session))
session <- list(ns = identity)
ax <- ax_states(ax, active = list(
allowMultipleDataPointsSelection = isTRUE(multiple),
filter = list(
type = effect_type,
value = effect_value
)
))
ax$x$shinyEvents$click <- list(
inputId = session$ns(inputId)
)
ax
}
#' Retrieve zoom information in Shiny
#'
#' @template ax-default
#' @param inputId The id that will be used server-side for retrieving zoom.
#' @param session The Shiny session.
#'
#' @note If x-axis is of type datetime, value retrieved is of class \code{POSIXct}.
#'
#' @export
#'
#' @importFrom shiny getDefaultReactiveDomain
#'
#' @examples
#' if (interactive()) {
#'
#' run_demo_input("zoom")
#'
#' }
set_input_zoom <- function(ax, inputId,
session = shiny::getDefaultReactiveDomain()) {
if (is.null(session))
session <- list(ns = identity)
ax$x$shinyEvents$zoomed <- list(
inputId = session$ns(inputId)
)
ax
}
#' Retrieve selection information in Shiny
#'
#' @template ax-default
#' @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,
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
}
#' Retrieve chart's base64 dataURI.
#'
#' @template ax-default
#' @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,
session = shiny::getDefaultReactiveDomain()) { # nocov start
if (is.null(session))
session <- list(ns = identity)
ax$x$shinyEvents$export <- list(
inputId = session$ns(inputId)
)
ax
} # nocov end
# Demo --------------------------------------------------------------------
# nocov start
#' Run Shiny input events examples
#'
#' @param example Name of the example.
#'
#' @export
#'
#' @importFrom shiny shinyAppFile
#'
#' @examples
#' if (interactive()) {
#'
#' run_demo_input("click")
#' run_demo_input("zoom")
#' run_demo_input("selection")
#'
#' }
run_demo_input <- function(example = c("click", "zoom", "selection")) {
example <- match.arg(example)
shiny::shinyAppFile(
appFile = system.file("examples/input", example, "app.R", package = "apexcharter"),
options = list("display.mode" = "showcase")
)
}
#' Run Shiny synchronization example
#'
#' @export
#'
#' @importFrom shiny shinyAppFile
#'
#' @examples
#' if (interactive()) {
#'
#' run_demo_sync()
#'
#' }
run_demo_sync <- function() {
shiny::shinyAppFile(
appFile = system.file("examples/sync", "app.R", package = "apexcharter"),
options = list("display.mode" = "showcase")
)
}
#' Run Shiny spark boxes example
#'
#' @export
#'
#' @importFrom shiny shinyAppFile
#'
#' @examples
#' if (interactive()) {
#'
#' run_demo_sparkbox()
#'
#' }
run_demo_sparkbox <- function() {
shiny::shinyAppFile(
appFile = system.file("examples/spark", "app.R", package = "apexcharter"),
options = list("display.mode" = "showcase")
)
}
# nocov end