Skip to content
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

Get event_data working on shiny modules #700

Merged
merged 10 commits into from
Sep 12, 2016
Merged

Get event_data working on shiny modules #700

merged 10 commits into from
Sep 12, 2016

Conversation

cpsievert
Copy link
Collaborator

@cpsievert cpsievert commented Aug 31, 2016

This PR addresses #659.

I see at least two ways to approach this problem:

(1) Have plot_ly() and event_data() both accept/query a shiny session object and enforce namespacing under-the-hood using that object. This will make it impossible to obtain data from events originating in another module. This seems to be the main motivation behind using modules anyway, so I think this is the preferred approach, and you can see the implementation in d2257d4

(2) Have event_data() always look in the global scope and leave it to the user to "namespace" the source arguments. This would conflict with the philosophy of using modules, but it wouldn't be that onerous on users, and saves us some hacks.

See here for a toy example. Any thoughts @jcheng5? @happyshows?

callModule(viz, "one", session = session)
callModule(viz, "two", session = session)
}

Copy link
Collaborator Author

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?

Copy link
Collaborator

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.

Copy link
Collaborator

@jcheng5 jcheng5 Sep 6, 2016

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.

Copy link
Collaborator

@jcheng5 jcheng5 Sep 6, 2016

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 a source = "A" param that's passed to plot_ly and event_data; that way, the main UI can decide whether reusableUI("one") and reusableUI("two") are linked together or separately, by passing the same (or default) or different source values.

@@ -119,6 +120,10 @@ plot_ly <- function(data = data.frame(), ..., type = NULL,
id <- new_id()
# avoid weird naming clashes
plotlyVisDat <- data
# automatically namespace source
if (!is.null(session)) {
source <- session$ns(source)
Copy link
Collaborator

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?

Copy link
Collaborator Author

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

JSON.stringify(d)
);
var src = ".clientValue-" + eventType + "-" + x.source;
Shiny.onInputChange(src, JSON.stringify(d));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason you're doing JSON.stringify() and jsonlite::fromJSON explicitly? If you just pass d from here, and don't JSON-decode from R, I think it should work fine--we do the JSON encoding for you.

Copy link
Collaborator Author

@cpsievert cpsievert Sep 6, 2016

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

@cpsievert cpsievert merged commit 238780b into master Sep 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants