From ed02df7c29782f5a286a18967d9997634d66a03b Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Thu, 17 Mar 2016 17:17:08 +1100 Subject: [PATCH 1/3] Add plotly_relayout to shiny input events --- R/shiny.R | 6 +++--- inst/examples/plotlyEvents/app.R | 8 +++++++- inst/htmlwidgets/plotly.js | 7 +++++++ man/event_data.Rd | 6 +++--- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/R/shiny.R b/R/shiny.R index 54d5bca5cd..4ad38425e2 100644 --- a/R/shiny.R +++ b/R/shiny.R @@ -36,7 +36,7 @@ renderPlotly <- function(expr, env = parent.frame(), quoted = FALSE) { #' This function must be called within a reactive shiny context. #' #' @param event The type of plotly event. Currently 'plotly_hover', -#' 'plotly_click', and 'plotly_selected' are supported. +#' 'plotly_click', 'plotly_selected', and 'plotly_relayout' are supported. #' @param source Which plot should the listener be tied to? This #' (character string) should match the value of \code{source} in \link{plot_ly}. #' @export @@ -45,8 +45,8 @@ renderPlotly <- function(expr, env = parent.frame(), quoted = FALSE) { #' shiny::runApp(system.file("examples", "events", package = "plotly")) #' } -event_data <- function(event = c("plotly_hover", "plotly_click", "plotly_selected"), - source = "A") { +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", diff --git a/inst/examples/plotlyEvents/app.R b/inst/examples/plotlyEvents/app.R index 13f3e9ba66..a140df4666 100644 --- a/inst/examples/plotlyEvents/app.R +++ b/inst/examples/plotlyEvents/app.R @@ -6,7 +6,8 @@ ui <- fluidPage( plotlyOutput("plot"), verbatimTextOutput("hover"), verbatimTextOutput("click"), - verbatimTextOutput("brush") + verbatimTextOutput("brush"), + verbatimTextOutput("zoom") ) server <- function(input, output, session) { @@ -39,6 +40,11 @@ server <- function(input, output, session) { if (is.null(d)) "Click and drag events (i.e., select/lasso) appear here (double-click to clear)" else d }) + output$zoom <- renderPrint({ + d <- event_data("plotly_relayout") + if (is.null(d)) "Relayout (i.e., zoom) events appear here" else d + }) + } shinyApp(ui, server, options = list(display.mode = "showcase")) diff --git a/inst/htmlwidgets/plotly.js b/inst/htmlwidgets/plotly.js index 8dbf13fd8b..e1d4a9de5f 100644 --- a/inst/htmlwidgets/plotly.js +++ b/inst/htmlwidgets/plotly.js @@ -71,6 +71,13 @@ HTMLWidgets.widget({ // send user input event data to shiny if (shinyMode) { + // https://plot.ly/javascript/zoom-events/ + graphDiv.on('plotly_relayout', function(d) { + Shiny.onInputChange( + ".clientValue-" + "plotly_relayout" + "-" + x.source, + JSON.stringify(d) + ); + }); graphDiv.on('plotly_hover', sendEventData('plotly_hover')); graphDiv.on('plotly_click', sendEventData('plotly_click')); graphDiv.on('plotly_selected', sendEventData('plotly_selected')); diff --git a/man/event_data.Rd b/man/event_data.Rd index 6898b8cb04..2fb921ae71 100644 --- a/man/event_data.Rd +++ b/man/event_data.Rd @@ -4,12 +4,12 @@ \alias{event_data} \title{Access plotly user input event data in shiny} \usage{ -event_data(event = c("plotly_hover", "plotly_click", "plotly_selected"), - source = "A") +event_data(event = c("plotly_hover", "plotly_click", "plotly_selected", + "plotly_relayout"), source = "A") } \arguments{ \item{event}{The type of plotly event. Currently 'plotly_hover', -'plotly_click', and 'plotly_selected' are supported.} +'plotly_click', 'plotly_selected', and 'plotly_relayout' are supported.} \item{source}{Which plot should the listener be tied to? This (character string) should match the value of \code{source} in \link{plot_ly}.} From 59928fce1f93ed04bda9cd2978c6ed6175ae7326 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Thu, 17 Mar 2016 17:30:34 +1100 Subject: [PATCH 2/3] bump version; update news --- DESCRIPTION | 2 +- NEWS | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 62c4b89228..13936cc4c7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: plotly Title: Create Interactive Web Graphics via 'plotly.js' -Version: 3.4.5 +Version: 3.4.6 Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"), email = "cpsievert1@gmail.com"), person("Chris", "Parmer", role = c("aut", "cph"), diff --git a/NEWS b/NEWS index 40dc7e02d3..24e8ca2ae6 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,11 @@ +3.4.6 -- 17 Mar 2016 + +NEW FEATURES: + +The 'plotly_relayout' event is now accessible via the event_data() function. + +Fixed #514. + 3.4.5 -- 17 Mar 2016 BUGFIX: From 2646cf6acbed60f1b17b24844647df760ea9ae4d Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Thu, 17 Mar 2016 17:34:21 +1100 Subject: [PATCH 3/3] fix silly test mistake --- tests/testthat/test-ggplot-point.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-ggplot-point.R b/tests/testthat/test-ggplot-point.R index f283983152..4ebb6bcb2a 100644 --- a/tests/testthat/test-ggplot-point.R +++ b/tests/testthat/test-ggplot-point.R @@ -49,6 +49,6 @@ test_that("tickvals/ticktext are appropriately boxed", { d <- data.frame(x = factor(75), y = 10) p <- qplot(x, y, data = d) info <- save_outputs(p, "point-box") - expect_true(plotly::to_JSON(info$layout$xaxis$tickvals) == '[1]') - expect_true(plotly::to_JSON(info$layout$xaxis$ticktext) == '["75"]') + expect_true(plotly:::to_JSON(info$layout$xaxis$tickvals) == '[1]') + expect_true(plotly:::to_JSON(info$layout$xaxis$ticktext) == '["75"]') })