-
Notifications
You must be signed in to change notification settings - Fork 627
/
shiny.R
92 lines (87 loc) · 3.35 KB
/
shiny.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
#' Shiny bindings for plotly
#'
#' Output and render functions for using plotly within Shiny
#' applications and interactive Rmd documents.
#'
#' @param outputId output variable to read from
#' @param width,height Must be a valid CSS unit (like \code{"100\%"},
#' `"400px"`, `"auto"`) or a number, which will be coerced to a
#' string and have `"px"` appended.
#' @param inline use an inline (`span()`) or block container
#' (`div()`) for the output
#' @param expr An expression that generates a plotly
#' @param env The environment in which to evaluate `expr`.
#' @param quoted Is `expr` a quoted expression (with `quote()`)? This
#' is useful if you want to save an expression in a variable.
#'
#' @importFrom htmlwidgets shinyWidgetOutput
#' @importFrom htmlwidgets shinyRenderWidget
#' @name plotly-shiny
#'
#' @export
plotlyOutput <- function(outputId, width = "100%", height = "400px",
inline = FALSE) {
htmlwidgets::shinyWidgetOutput(
outputId = outputId,
name = "plotly",
width = width,
height = height,
inline = inline,
package = "plotly",
reportSize = TRUE
)
}
#' @rdname plotly-shiny
#' @export
renderPlotly <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) { expr <- substitute(expr) } # force quoted
# this makes it possible to pass a ggplot2 object to renderPlotly()
# https://github.com/ramnathv/htmlwidgets/issues/166#issuecomment-153000306
expr <- as.call(list(call(":::", quote("plotly"), quote("prepareWidget")), expr))
renderFunc <- shinyRenderWidget(expr, plotlyOutput, env, quoted = TRUE)
# remove 'internal' plotly attributes that are known to cause false
# positive test results in shinytest (snapshotPreprocessOutput was added
# in shiny 1.0.3.9002, but we require >= 1.1)
shiny::snapshotPreprocessOutput(
renderFunc,
function(value) {
json <- from_JSON_safe(value)
json$x <- json$x[!names(json$x) %in% c("visdat", "cur_data", "attrs")]
to_JSON(json)
}
)
}
# Converts a plot, OR a promise of a plot, to plotly
prepareWidget <- function(x) {
if (promises::is.promising(x)) {
promises::then(x, ggplotly)
} else {
ggplotly(x)
}
}
#' Access plotly user input event data in shiny
#'
#' This function must be called within a reactive shiny context.
#'
#' @param event The type of plotly event. Currently 'plotly_hover',
#' 'plotly_click', 'plotly_selected', and 'plotly_relayout' are supported.
#' @param source a character string of length 1. Match the value of this string
#' with the source argument in [plot_ly()] to retrieve the
#' event data corresponding to a specific plot (shiny apps can have multiple plots).
#' @param session a shiny session object (the default should almost always be used).
#' @export
#' @author Carson Sievert
#' @examples \dontrun{
#' plotly_example("shiny", "event_data")
#' }
event_data <- function(event = c("plotly_hover", "plotly_click", "plotly_selected",
"plotly_relayout"), source = "A",
session = shiny::getDefaultReactiveDomain()) {
if (is.null(session)) {
stop("No reactive domain detected. This function can only be called \n",
"from within a reactive shiny context.")
}
src <- sprintf(".clientValue-%s-%s", event[1], source)
val <- session$rootScope()$input[[src]]
if (is.null(val)) val else jsonlite::fromJSON(val)
}