Skip to content

Commit

Permalink
Use new plotly::event_data() mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert committed Jan 21, 2016
1 parent 0c1d9ef commit 5633b6e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
12 changes: 5 additions & 7 deletions plotlyLinkedBrush/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ library(shiny)
ui <- fluidPage(
titlePanel("Linked highlighting with plotly and shiny"),
mainPanel(
plotlyOutput("x", width = 400, height = 250, inline = T),
htmltools::div(style = "display:inline-block", plotlyOutput("x", width = 400, height = 250)),
wellPanel(
style = "display:inline-block; vertical-align:top;",
sliderInput("xbins", "Number of x bins",
Expand All @@ -14,8 +14,8 @@ ui <- fluidPage(
min = 1, max = 50, value = 20, width = 250)
),
br(),
plotlyOutput("xy", width = 400, height = 400, inline = T),
plotlyOutput("y", width = 250, height = 400, inline = T)
htmltools::div(style = "display:inline-block", plotlyOutput("xy", width = 400, height = 400)),
htmltools::div(style = "display:inline-block", plotlyOutput("y", width = 250, height = 400))
)
)

Expand All @@ -24,8 +24,6 @@ m <- list(color = toRGB("black"))
m2 <- list(color = toRGB("black", 0.2))

server <- function(input, output, session) {
# mechanism to access the 'plotly_selected' user event
cv <- crosstalk::ClientValue$new("plotly_selected", group = "A")

# convenience function for computing xbin/ybin object given a number of bins
compute_bins <- function(x, n) {
Expand All @@ -43,7 +41,7 @@ server <- function(input, output, session) {
p <- plot_ly(x = x, type = "histogram", autobinx = F,
xbins = xbins, marker = m2)
# obtain plotlyjs selection
s <- cv$get()
s <- event_data("plotly_selected")
# if points are selected, subset the data, and highlight
if (length(s$x) > 0) {
p <- add_trace(p, x = s$x, type = "histogram", autobinx = F,
Expand All @@ -61,7 +59,7 @@ server <- function(input, output, session) {
ybins <- compute_bins(y, input$ybins)
p <- plot_ly(y = y, type = "histogram", autobiny = F,
ybins = ybins, marker = m2)
s <- cv$get()
s <- event_data("plotly_selected")
if (length(s$y) > 0) {
p <- add_trace(p, y = s$y, type = "histogram", autobiny = F,
ybins = ybins, marker = m)
Expand Down
8 changes: 2 additions & 6 deletions plotlyLinkedClick/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ server <- function(input, output, session) {
yaxis = list(title = ""))
})

# the 'group' here should match the 'set' in plot_ly()
# (this naming is likely to change)
cv <- crosstalk::ClientValue$new("plotly_click", group = "A")

output$selection <- renderPrint({
s <- cv$get()
s <- event_data("plotly_click")
if (length(s) == 0) {
"Click on a cell in the heatmap to display a scatterplot"
} else {
Expand All @@ -36,7 +32,7 @@ server <- function(input, output, session) {
})

output$scatterplot <- renderPlotly({
s <- cv$get()
s <- event_data("plotly_click")
if (length(s)) {
vars <- c(s[["x"]], s[["y"]])
d <- setNames(mtcars[vars], c("x", "y"))
Expand Down

2 comments on commit 5633b6e

@dgrapov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
This is a great example, Thank you!

However I can't seem to find plotly::event_data()

plotly::event_data()
Error: 'event_data' is not an exported object from 'namespace:plotly'

Trying the crosstalk version I get no reactivity. What version of plotly does this work with or am I missing something else?

 sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] crosstalk_0.1.0 shiny_0.13.0    plotly_2.3.0    ggplot2_2.0.0  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.3      knitr_1.12       magrittr_1.5     devtools_1.9.1   munsell_0.4.2   
 [6] xtable_1.7-4     colorspace_1.2-6 R6_2.1.1         stringr_1.0.0    httr_1.0.0      
[11] plyr_1.8.3       tools_3.2.0      grid_3.2.0       gtable_0.1.2     htmltools_0.3   
[16] yaml_2.1.13      digest_0.6.9     gridExtra_2.0.0  base64enc_0.1-3  htmlwidgets_0.5 
[21] viridis_0.3.2    curl_0.9.4       mime_0.4         memoise_0.2.1    stringi_1.0-1   
[26] scales_0.3.0     jsonlite_0.9.19  httpuv_1.3.3    
> 

Thanks,

@dgrapov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found event_data() on the branch feature/transmit.

Thanks!

Please sign in to comment.