From 7325807a2c767bf1da8f6f373fceb5640b1cfbeb Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Thu, 15 Nov 2018 16:23:35 -0600 Subject: [PATCH 1/2] don't change 'plotly_selected' shiny input if event data is falsy. Closes #1121. Closes #1122 --- inst/htmlwidgets/plotly.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/inst/htmlwidgets/plotly.js b/inst/htmlwidgets/plotly.js index 5babe83890..c8edb24453 100644 --- a/inst/htmlwidgets/plotly.js +++ b/inst/htmlwidgets/plotly.js @@ -289,10 +289,12 @@ HTMLWidgets.widget({ ); }); graphDiv.on('plotly_selected', function(d) { - Shiny.onInputChange( - ".clientValue-plotly_selected-" + x.source, - JSON.stringify(eventDataWithKey(d)) - ); + if (d) { + Shiny.onInputChange( + ".clientValue-plotly_selected-" + x.source, + JSON.stringify(eventDataWithKey(d)) + ); + } }); graphDiv.on('plotly_unhover', function(eventData) { Shiny.onInputChange(".clientValue-plotly_hover-" + x.source, null); From 1cccba54e4ba55be0b0c3c33ace2765a5395bf03 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Thu, 15 Nov 2018 16:43:23 -0600 Subject: [PATCH 2/2] add a comment --- inst/htmlwidgets/plotly.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/inst/htmlwidgets/plotly.js b/inst/htmlwidgets/plotly.js index c8edb24453..df0483dcf8 100644 --- a/inst/htmlwidgets/plotly.js +++ b/inst/htmlwidgets/plotly.js @@ -289,6 +289,12 @@ HTMLWidgets.widget({ ); }); graphDiv.on('plotly_selected', function(d) { + // If 'plotly_selected' has already been fired, and you click + // on the plot afterwards, this event fires `undefined`?!? + // That might be considered a plotly.js bug, but it doesn't make + // sense for this input change to occur if `d` is falsy because, + // even in the empty selection case, `d` is truthy (an object), + // and the 'plotly_deselect' event will reset this input if (d) { Shiny.onInputChange( ".clientValue-plotly_selected-" + x.source,