Skip to content

Commit 238780b

Browse files
authored
Merge pull request #700 from ropensci/fix/ns
Get event_data working on shiny modules
2 parents 99c7368 + e6f2a81 commit 238780b

File tree

7 files changed

+69
-10
lines changed

7 files changed

+69
-10
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: plotly
22
Title: Create Interactive Web Graphics via 'plotly.js'
3-
Version: 4.3.6
3+
Version: 4.3.7
44
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
55
email = "[email protected]"),
66
person("Chris", "Parmer", role = c("aut", "cph"),
@@ -44,7 +44,7 @@ Suggests:
4444
testthat,
4545
knitr,
4646
devtools,
47-
shiny,
47+
shiny (>= 0.14),
4848
htmltools,
4949
curl,
5050
rmarkdown,

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 4.3.7 -- 11 September 2016
2+
3+
## BUG FIXES
4+
5+
* `event_data()` now works inside shiny modules (#659). For an example, see <https://github.com/ropensci/plotly/tree/master/inst/examples/plotlyShinyModules>
6+
17
# 4.3.6 -- 9 September 2016
28

39
## CHANGES

R/plotly_build.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ plotly_build.plotly <- function(p) {
9898
})
9999
dataArrayAttrs <- names(Attrs)[as.logical(isArray)]
100100
tr <- trace[names(trace) %in% c(npscales(), special_attrs(trace), dataArrayAttrs)]
101+
# TODO: does it make sense to "train" matrices/2D-tables (e.g. z)?
102+
tr <- tr[vapply(tr, function(x) is.null(dim(x)), logical(1))]
101103
builtData <- tibble::as_tibble(tr)
102104

103105
# avoid clobbering I() (i.e., variables that shouldn't be scaled)

R/shiny.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,21 @@ renderPlotly <- function(expr, env = parent.frame(), quoted = FALSE) {
4141
#' @param source Which plot should the listener be tied to? This
4242
#' (character string) should match the value of \code{source} in
4343
#' \code{\link{plot_ly}()}.
44+
#' @param session a shiny session object (the default should almost always be used).
4445
#' @export
4546
#' @author Carson Sievert
4647
#' @examples \dontrun{
4748
#' shiny::runApp(system.file("examples", "plotlyEvents", package = "plotly"))
4849
#' }
4950

5051
event_data <- function(event = c("plotly_hover", "plotly_click", "plotly_selected",
51-
"plotly_relayout"), source = "A") {
52-
session <- shiny::getDefaultReactiveDomain()
52+
"plotly_relayout"), source = "A",
53+
session = shiny::getDefaultReactiveDomain()) {
5354
if (is.null(session)) {
5455
stop("No reactive domain detected. This function can only be called \n",
5556
"from within a reactive shiny context.")
5657
}
57-
val <- session$input[[sprintf(".clientValue-%s-%s", event[1], source)]]
58+
src <- sprintf(".clientValue-%s-%s", event[1], source)
59+
val <- session$rootScope()$input[[src]]
5860
if (is.null(val)) val else jsonlite::fromJSON(val)
5961
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
library(shiny)
2+
library(plotly)
3+
4+
reusableUI <- function(id = NULL) {
5+
ns <- NS(id)
6+
7+
fluidRow(
8+
column(4, plotlyOutput(ns("p1"))),
9+
column(4, plotlyOutput(ns("p2"))),
10+
column(4, verbatimTextOutput(ns("ev1"))),
11+
column(4, verbatimTextOutput(ns("ev2")))
12+
)
13+
}
14+
15+
viz <- function(input, output, session, src) {
16+
17+
# if you want, you can distinguish between events *within* a module
18+
src2 <- paste0(src, "2")
19+
20+
output$p1 <- renderPlotly({
21+
plot_ly(mtcars, x = ~mpg, y = ~disp,
22+
key = row.names(mtcars), source = src)
23+
})
24+
output$p2 <- renderPlotly({
25+
plot_ly(mtcars, x = ~mpg, y = ~disp,
26+
key = row.names(mtcars), source = src2)
27+
})
28+
output$ev1 <- renderPrint({
29+
event_data("plotly_hover", source = src)
30+
})
31+
output$ev2 <- renderPrint({
32+
event_data("plotly_hover", source = src2)
33+
})
34+
35+
}
36+
37+
ui <- fluidPage(
38+
reusableUI("one"),
39+
reusableUI("two")
40+
)
41+
42+
server <- function(input, output, session) {
43+
# use the src argument to namespace plotly events
44+
callModule(viz, "one", src = "A")
45+
callModule(viz, "two", src = "B")
46+
}
47+
48+
shinyApp(ui, server)

inst/htmlwidgets/plotly.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ HTMLWidgets.widget({
6868
attachKey("key");
6969
return obj;
7070
});
71-
Shiny.onInputChange(
72-
".clientValue-" + eventType + "-" + x.source,
73-
JSON.stringify(d)
74-
);
71+
var src = ".clientValue-" + eventType + "-" + x.source;
72+
Shiny.onInputChange(src, JSON.stringify(d));
7573
};
7674
};
7775

man/event_data.Rd

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

0 commit comments

Comments
 (0)