-
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 5 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,39 @@ | ||
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("ev"))) | ||
) | ||
} | ||
|
||
viz <- function(input, output, session) { | ||
output$p1 <- renderPlotly({ | ||
plot_ly(mtcars, x = ~mpg, y = ~disp, | ||
key = row.names(mtcars), session = session) | ||
}) | ||
output$p2 <- renderPlotly({ | ||
plot_ly(mtcars, x = ~mpg, y = ~disp, | ||
key = row.names(mtcars), session = session) | ||
}) | ||
output$ev <- renderPrint({ | ||
d <- event_data("plotly_hover", session = session) | ||
if (is.null(d)) print(paste("Module", session$ns(NULL))) else d | ||
}) | ||
} | ||
|
||
ui <- fluidPage( | ||
reusableUI("one"), | ||
reusableUI("two") | ||
) | ||
|
||
server <- function(input, output, session) { | ||
callModule(viz, "one", session = session) | ||
callModule(viz, "two", session = session) | ||
} | ||
|
||
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. @jcheng5 this example seems to still work without passing the session to 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. No, you generally don't want to be explicitly passing the session--just let it use the default. 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. Same goes for the callModule calls, they shouldn't need explicit session arguments passed to them either. 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. Oh I think what I said to you last week was that you should explicitly pass the source, not the session. So |
||
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. |
||
}; | ||
}; | ||
|
||
|
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.
Why do you want the source namespaced? Don't you want to be able to have multiple modules share the same source?
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.
I was thinking namespacing would be imposed by default by plotly, but I think I'm now in favor of the approach you describe below