-
Notifications
You must be signed in to change notification settings - Fork 634
Get event_data working on shiny modules #700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
3a1f221
d2257d4
d66e2ca
308ff92
4d8788b
ffef491
dca6b82
3df8f86
3536fe7
e6f2a81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
library(shiny) | ||
library(plotly) | ||
|
||
reusableUI <- function(id = NULL) { | ||
ns <- NS(id) | ||
|
||
fluidRow( | ||
column(4, plotlyOutput(ns("p1"))), | ||
column(4, plotlyOutput(ns("p2"))), | ||
column(4, verbatimTextOutput(ns("ev1"))), | ||
column(4, verbatimTextOutput(ns("ev2"))) | ||
) | ||
} | ||
|
||
viz <- function(input, output, scope, src) { | ||
|
||
# if you want, you can define multiple sources here | ||
src2 <- paste0(src, "2") | ||
|
||
output$p1 <- renderPlotly({ | ||
plot_ly(mtcars, x = ~mpg, y = ~disp, | ||
key = row.names(mtcars), source = src) | ||
}) | ||
output$p2 <- renderPlotly({ | ||
plot_ly(mtcars, x = ~mpg, y = ~disp, | ||
key = row.names(mtcars), source = src2) | ||
}) | ||
output$ev1 <- renderPrint({ | ||
event_data("plotly_hover", source = src) | ||
}) | ||
output$ev2 <- renderPrint({ | ||
event_data("plotly_hover", source = src2) | ||
}) | ||
|
||
} | ||
|
||
ui <- fluidPage( | ||
reusableUI("one"), | ||
reusableUI("two") | ||
) | ||
|
||
server <- function(input, output, session) { | ||
# use the src argument to namespace plotly events | ||
callModule(viz, "one", src = "A") | ||
callModule(viz, "two", src = "B") | ||
} | ||
|
||
shinyApp(ui, server) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,10 +62,8 @@ HTMLWidgets.widget({ | |
attachKey("key"); | ||
return obj; | ||
}); | ||
Shiny.onInputChange( | ||
".clientValue-" + eventType + "-" + x.source, | ||
JSON.stringify(d) | ||
); | ||
var src = ".clientValue-" + eventType + "-" + x.source; | ||
Shiny.onInputChange(src, JSON.stringify(d)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason you're doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm pretty sure I did that because the default translates an array of objects to a named character vector, when I really want a data frame. Would you go about ensuring a data frame in another way? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's fair. It does mean you are sending double-encoded JSON data across the wire which will bloat things up a little, but I guess these are pretty small packets anyway. |
||
}; | ||
}; | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jcheng5 this example seems to still work without passing the session to
plot_ly()
/event_data()
. Would u still suggest doing so?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, you generally don't want to be explicitly passing the session--just let it use the default.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same goes for the callModule calls, they shouldn't need explicit session arguments passed to them either.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I think what I said to you last week was that you should explicitly pass the source, not the session. So
viz
would have asource = "A"
param that's passed toplot_ly
andevent_data
; that way, the main UI can decide whetherreusableUI("one")
andreusableUI("two")
are linked together or separately, by passing the same (or default) or different source values.