Skip to content

Commit 8c5cd8e

Browse files
committed
Bump version, update news, add support for hover and clear events
1 parent b2c459e commit 8c5cd8e

File tree

6 files changed

+70
-20
lines changed

6 files changed

+70
-20
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: plotly
22
Title: Create Interactive Web Graphics via Plotly's JavaScript Graphing Library
3-
Version: 2.4.4
3+
Version: 2.5.0
44
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
55
email = "[email protected]"),
66
person("Chris", "Parmer", role = c("aut", "cph"),

NEWS

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
2.5.0 -- 1 Mar 2015
2+
3+
NEW FEATURES
4+
5+
* New event_data() function provides easy access to plotly events in shiny.
6+
For an example, see https://github.com/ropensci/plotly/tree/master/inst/examples/plotlyEvents
7+
8+
* plot_ly() and ggplotly() gain a source argument to differentiate between
9+
plotly events in shiny apps with multiple plots. ggplotly() also gains width
10+
and height arguments.
11+
12+
CHANGES
13+
14+
The arguments filename, fileopt, world_readable in ggplotly() were removed as
15+
they should be provided to plotly_POST() instead.
16+
117
2.4.4 -- 13 Feb 2015
218

319
as.widget() now returns htmlwidget objects untouched. See #449.

R/shiny.R

+3-4
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ renderPlotly <- function(expr, env = parent.frame(), quoted = FALSE) {
3535
#'
3636
#' This function must be called within a reactive shiny context.
3737
#'
38-
#' @param event The type of plotly event. Currently 'plotly_click' and
39-
#' 'plotly_selected' are supported.
38+
#' @param event The type of plotly event. Currently 'plotly_hover',
39+
#' 'plotly_click', and 'plotly_selected' 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,14 +45,13 @@ 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_click", "plotly_selected"),
48+
event_data <- function(event = c("plotly_hover", "plotly_click", "plotly_selected"),
4949
source = "A") {
5050
session <- shiny::getDefaultReactiveDomain()
5151
if (is.null(session)) {
5252
stop("No reactive domain detected. This function can only be called \n",
5353
"from within a reactive shiny context.")
5454
}
55-
# TODO: is this safe?
5655
val <- session$input[[sprintf(".clientValue-%s-%s", event[1], source)]]
5756
if (event[1] == "plotly_selected" && !is.null(val)) {
5857
data.frame(lapply(val, as.numeric))

inst/examples/plotlyEvents/app.R

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ library(plotly)
44
ui <- fluidPage(
55
radioButtons("plotType", "Plot Type:", choices = c("ggplotly", "plotly")),
66
plotlyOutput("plot"),
7+
verbatimTextOutput("hover"),
78
verbatimTextOutput("click"),
89
verbatimTextOutput("brush")
910
)
@@ -20,14 +21,19 @@ server <- function(input, output, session) {
2021
}
2122
})
2223

24+
output$hover <- renderPrint({
25+
d <- event_data("plotly_hover")
26+
if (is.null(d)) "Hover events appear here (unhover to clear)" else d
27+
})
28+
2329
output$click <- renderPrint({
2430
d <- event_data("plotly_click")
25-
if (is.null(d)) "Click on a point to view event data" else d
31+
if (is.null(d)) "Click events appear here (double-click to clear)" else d
2632
})
2733

2834
output$brush <- renderPrint({
2935
d <- event_data("plotly_selected")
30-
if (is.null(d)) "Click and drag to view event data" else d
36+
if (is.null(d)) "Click and drag events (i.e., select/lasso) appear here (double-click to clear)" else d
3137
})
3238

3339
}

inst/htmlwidgets/plotly.js

+38-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ HTMLWidgets.widget({
88
},
99

1010
resize: function(el, width, height, instance) {
11-
// TODO: impose fixed coordinates, if specified (see #342)
1211
Plotly.relayout(el.id, {width: width, height: height});
1312
},
1413

@@ -52,10 +51,38 @@ HTMLWidgets.widget({
5251
}
5352
return obj;
5453
});
55-
Shiny.onInputChange(
56-
".clientValue-plotly_click-" + x.source,
57-
d
58-
);
54+
Shiny.onInputChange(".clientValue-plotly_click-" + x.source, d);
55+
});
56+
57+
// clear click selection
58+
graphDiv.on('plotly_doubleclick', function(eventData) {
59+
Shiny.onInputChange(".clientValue-plotly_click-" + x.source, null);
60+
});
61+
62+
graphDiv.on('plotly_hover', function(eventData) {
63+
// extract only the data we may want to access in R
64+
var d = eventData.points.map(function(pt) {
65+
var obj = {
66+
curveNumber: pt.curveNumber,
67+
pointNumber: pt.pointNumber,
68+
x: pt.x,
69+
y: pt.y
70+
};
71+
if (pt.data.hasOwnProperty("key")) {
72+
if (typeof pt.pointNumber === "number") {
73+
obj.key = pt.data.key[pt.pointNumber];
74+
} else {
75+
obj.key = pt.data.key[pt.pointNumber[0]][pt.pointNumber[1]];
76+
} // TODO: can pointNumber be 3D?
77+
}
78+
return obj;
79+
});
80+
Shiny.onInputChange(".clientValue-plotly_hover-" + x.source, d);
81+
});
82+
83+
// clear hover selection
84+
graphDiv.on('plotly_unhover', function(eventData) {
85+
Shiny.onInputChange(".clientValue-plotly_hover-" + x.source, null);
5986
});
6087

6188
graphDiv.on('plotly_selected', function(eventData) {
@@ -69,14 +96,15 @@ HTMLWidgets.widget({
6996
x: pts.map(function(pt) {return pt.x; }),
7097
y: pts.map(function(pt) {return pt.y; })
7198
};
72-
Shiny.onInputChange(
73-
".clientValue-plotly_selected-" + x.source,
74-
obj
75-
);
99+
Shiny.onInputChange(".clientValue-plotly_selected-" + x.source, obj);
76100
}
77101
});
78102

79-
// TODO: send `null` on a double-click to clear input value!?
103+
// clear select/lasso selection & click
104+
graphDiv.on('plotly_deselect', function(eventData) {
105+
Shiny.onInputChange(".clientValue-plotly_selected-" + x.source, null);
106+
Shiny.onInputChange(".clientValue-plotly_click-" + x.source, null);
107+
});
80108

81109
} // shinyMode
82110
} // renderValue

man/event_data.Rd

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)