Skip to content

Commit ed02df7

Browse files
committed
Add plotly_relayout to shiny input events
1 parent 37c8c49 commit ed02df7

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

R/shiny.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ renderPlotly <- function(expr, env = parent.frame(), quoted = FALSE) {
3636
#' This function must be called within a reactive shiny context.
3737
#'
3838
#' @param event The type of plotly event. Currently 'plotly_hover',
39-
#' 'plotly_click', and 'plotly_selected' are supported.
39+
#' 'plotly_click', 'plotly_selected', and 'plotly_relayout' are supported.
4040
#' @param source Which plot should the listener be tied to? This
4141
#' (character string) should match the value of \code{source} in \link{plot_ly}.
4242
#' @export
@@ -45,8 +45,8 @@ renderPlotly <- function(expr, env = parent.frame(), quoted = FALSE) {
4545
#' shiny::runApp(system.file("examples", "events", package = "plotly"))
4646
#' }
4747

48-
event_data <- function(event = c("plotly_hover", "plotly_click", "plotly_selected"),
49-
source = "A") {
48+
event_data <- function(event = c("plotly_hover", "plotly_click", "plotly_selected",
49+
"plotly_relayout"), source = "A") {
5050
session <- shiny::getDefaultReactiveDomain()
5151
if (is.null(session)) {
5252
stop("No reactive domain detected. This function can only be called \n",

inst/examples/plotlyEvents/app.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ ui <- fluidPage(
66
plotlyOutput("plot"),
77
verbatimTextOutput("hover"),
88
verbatimTextOutput("click"),
9-
verbatimTextOutput("brush")
9+
verbatimTextOutput("brush"),
10+
verbatimTextOutput("zoom")
1011
)
1112

1213
server <- function(input, output, session) {
@@ -39,6 +40,11 @@ server <- function(input, output, session) {
3940
if (is.null(d)) "Click and drag events (i.e., select/lasso) appear here (double-click to clear)" else d
4041
})
4142

43+
output$zoom <- renderPrint({
44+
d <- event_data("plotly_relayout")
45+
if (is.null(d)) "Relayout (i.e., zoom) events appear here" else d
46+
})
47+
4248
}
4349

4450
shinyApp(ui, server, options = list(display.mode = "showcase"))

inst/htmlwidgets/plotly.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ HTMLWidgets.widget({
7171

7272
// send user input event data to shiny
7373
if (shinyMode) {
74+
// https://plot.ly/javascript/zoom-events/
75+
graphDiv.on('plotly_relayout', function(d) {
76+
Shiny.onInputChange(
77+
".clientValue-" + "plotly_relayout" + "-" + x.source,
78+
JSON.stringify(d)
79+
);
80+
});
7481
graphDiv.on('plotly_hover', sendEventData('plotly_hover'));
7582
graphDiv.on('plotly_click', sendEventData('plotly_click'));
7683
graphDiv.on('plotly_selected', sendEventData('plotly_selected'));

man/event_data.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)