This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathhandle_resize.R
78 lines (69 loc) · 2.03 KB
/
handle_resize.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
#' Handlers and interactive inputs for plot sizing.
#'
#' @param vis Visualisation to listen to.
#' @param on_resize Callback function with arguments:
#' \describe{
#' \item{width,height}{Width and height in pixels}
#' \item{padding}{A named list of four components giving the padding in
#' each direction}
#' \item{session}{The session, used to communicate with the browser}
#' }
#' @export
#' @examples
#' # This example just prints out the current dimensions to the console
#' mtcars %>% ggvis(~mpg, ~wt) %>%
#' layer_points() %>%
#' handle_resize(function(width, height, ...) cat(width, "x", height, "\n"))
#'
#' # Use plot_width() and plot_height() to dynamically get the plot size
#' # inside the plot.
#' mtcars %>% ggvis(~mpg, ~wt) %>% layer_text(text := plot_width())
#' mtcars %>% ggvis(~mpg, ~wt) %>% layer_text(text := plot_height())
handle_resize <- function(vis, on_resize) {
broker <- create_broker(
reactive(NULL),
connect = connect_resize(on_resize),
spec = list(type = "resize")
)
register_reactive(vis, broker)
}
#' @export
#' @rdname handle_resize
plot_width <- function(vis) {
vals <- shiny::reactiveValues()
vals$x <- 100
on_resize <- function(width, ...) {
vals$x <- width
}
create_broker(
reactive(vals$x),
connect = connect_resize(on_resize),
spec = list(type = "resize")
)
}
#' @export
#' @rdname handle_resize
plot_height <- function(vis) {
vals <- shiny::reactiveValues()
vals$x <- 100
on_resize <- function(height, ...) {
vals$x <- height
}
create_broker(
reactive(vals$x),
connect = connect_resize(on_resize),
spec = list(type = "resize")
)
}
connect_resize <- function(on_resize) {
check_callback(on_resize, c("width", "height", "padding", "session"))
function(session, plot_id) {
id <- paste0(plot_id, "_resize")
shiny::observe({
value <- session$input[[id]]
if (is.null(value)) return()
on_resize(width = value$width, height = value$height,
padding = value$padding, session = value$session)
})
}
}