Debian Codegraph @

R/image-interact-opts.R

raw
1 #' Control interactive plot point events
2 #'
3 #' These functions give control over the `click`, `dblClick` and
4 #' `hover` events generated by [imageOutput()] and [plotOutput()].
5 #'
6 #' @param id Input value name. For example, if the value is `"plot_click"`,
7 #' then the event data will be available as `input$plot_click`.
8 #' @param clip Should the click area be clipped to the plotting area? If
9 #' `FALSE`, then the server will receive click events even when the mouse is
10 #' outside the plotting area, as long as it is still inside the image.
11 #' @param delay For `dblClickOpts()`: the maximum delay (in ms) between a
12 #' pair clicks for them to be counted as a double-click.
13 #'
14 #' For `hoverOpts()`: how long to delay (in ms) when debouncing or throttling
15 #' before sending the mouse location to the server.
16 #' @param delayType The type of algorithm for limiting the number of hover
17 #' events. Use `"throttle"` to limit the number of hover events to one
18 #' every `delay` milliseconds. Use `"debounce"` to suspend events
19 #' while the cursor is moving, and wait until the cursor has been at rest for
20 #' `delay` milliseconds before sending an event.
21 #' @seealso [brushOpts()] for brushing events.
22 #' @export
23 clickOpts <- function(id, clip = TRUE) {
24 if (is.null(id))
25 stop("id must not be NULL")
26
27 list(
28 id = id,
29 clip = clip
30 )
31 }
32
33
34 #' @export
35 #' @rdname clickOpts
36 dblclickOpts <- function(id, clip = TRUE, delay = 400) {
37 if (is.null(id))
38 stop("id must not be NULL")
39
40 list(
41 id = id,
42 clip = clip,
43 delay = delay
44 )
45 }
46
47 #' @param nullOutside If `TRUE` (the default), the value will be set to
48 #' `NULL` when the mouse exits the plotting area. If `FALSE`, the
49 #' value will stop changing when the cursor exits the plotting area.
50 #' @export
51 #' @rdname clickOpts
52 hoverOpts <- function(id, delay = 300,
53 delayType = c("debounce", "throttle"), clip = TRUE,
54 nullOutside = TRUE) {
55 if (is.null(id))
56 stop("id must not be NULL")
57
58 list(
59 id = id,
60 delay = delay,
61 delayType = match.arg(delayType),
62 clip = clip,
63 nullOutside = nullOutside
64 )
65 }
66
67 #' Create an object representing brushing options
68 #'
69 #' This generates an object representing brushing options, to be passed as the
70 #' `brush` argument of [imageOutput()] or
71 #' [plotOutput()].
72 #'
73 #' @param id Input value name. For example, if the value is `"plot_brush"`,
74 #' then the coordinates will be available as `input$plot_brush`. Multiple
75 #' `imageOutput`/`plotOutput` calls may share the same `id`
76 #' value; brushing one image or plot will cause any other brushes with the
77 #' same `id` to disappear.
78 #' @param fill Fill color of the brush. If `'auto'`, it derives from the link
79 #' color of the plot's HTML container (if **thematic** is enabled, and `accent`
80 #' is a non-`'auto'` value, that color is used instead).
81 #' @param stroke Outline color of the brush. If `'auto'`, it derives from the
82 #' foreground color of the plot's HTML container (if **thematic** is enabled,
83 #' and `fg` is a non-`'auto'` value, that color is used instead).
84 #' @param opacity Opacity of the brush
85 #' @param delay How long to delay (in milliseconds) when debouncing or
86 #' throttling, before sending the brush data to the server.
87 #' @param delayType The type of algorithm for limiting the number of brush
88 #' events. Use `"throttle"` to limit the number of brush events to one
89 #' every `delay` milliseconds. Use `"debounce"` to suspend events
90 #' while the cursor is moving, and wait until the cursor has been at rest for
91 #' `delay` milliseconds before sending an event.
92 #' @param clip Should the brush area be clipped to the plotting area? If FALSE,
93 #' then the user will be able to brush outside the plotting area, as long as
94 #' it is still inside the image.
95 #' @param direction The direction for brushing. If `"xy"`, the brush can be
96 #' drawn and moved in both x and y directions. If `"x"`, or `"y"`,
97 #' the brush wil work horizontally or vertically.
98 #' @param resetOnNew When a new image is sent to the browser (via
99 #' [renderImage()]), should the brush be reset? The default,
100 #' `FALSE`, is useful if you want to update the plot while keeping the
101 #' brush. Using `TRUE` is useful if you want to clear the brush whenever
102 #' the plot is updated.
103 #' @seealso [clickOpts()] for clicking events.
104 #' @export
105 brushOpts <- function(id, fill = "#9cf", stroke = "#036",
106 opacity = 0.25, delay = 300,
107 delayType = c("debounce", "throttle"), clip = TRUE,
108 direction = c("xy", "x", "y"),
109 resetOnNew = FALSE) {
110 if (is.null(id))
111 stop("id must not be NULL")
112
113 if (identical(fill, "auto")) {
114 fill <- getThematicOption("accent", "auto")
115 }
116 if (identical(stroke, "auto")) {
117 stroke <- getThematicOption("fg", "auto")
118 }
119
120 list(
121 id = id,
122 fill = fill,
123 stroke = stroke,
124 opacity = opacity,
125 delay = delay,
126 delayType = match.arg(delayType),
127 clip = clip,
128 direction = match.arg(direction),
129 resetOnNew = resetOnNew
130 )
131 }
132
133 getThematicOption <- function(name = "", default = NULL, resolve = FALSE) {
134 if (isNamespaceLoaded("thematic")) {
135 # TODO: use :: once thematic is on CRAN
136 tgo <- utils::getFromNamespace("thematic_get_option", "thematic")
137 tgo(name = name, default = default, resolve = resolve)
138 } else {
139 default
140 }
141 }