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

Responding to altair chart selections #112

Open
joelostblom opened this issue Oct 24, 2023 · 3 comments
Open

Responding to altair chart selections #112

joelostblom opened this issue Oct 24, 2023 · 3 comments

Comments

@joelostblom
Copy link

joelostblom commented Oct 24, 2023

Does shiny support reactivity/events from making a selection in an interactive chart such as Altair, Plotly, Bokeh, etc? I can't seem to find any examples of this; the closest is the page on "Reacting to widget updates" seem to suggest that shiny only supports events from selections in charts that provide this via an already existing jupyter widgets interaction (such as on_click etc), is this correct?

In Altair, we recently added support for exactly this: chart selections can be accessed in notebooks through a jupyter widget chart object. Based on my understanding this would be supported in shiny too (and maybe even quarto dashboards since they also support jupyter widgets?), but I want to double check if this is the case before trying it out.

I think it could be valuable to add a section on chart selections driving events in the docs, just to be explicit about what works and what doesn't.

Somewhat related issue in posit-dev/py-shiny#768

@cpsievert
Copy link
Collaborator

Does shiny support reactivity/events from making a selection in an interactive chart such as Altair, Plotly, Bokeh, etc?

Yes, {shinywidgets}'s reactive_read() function supports reactively reading trait(s) of any ipywidget that supports an .observe() method. In fact, in that "Reacting to widget updates" section you reference, the reactive_read(map, "center") line essentially does a reactive version of map.observe(callback, "center")

Here is how you can leverage reactive_read() to respond to altair selections, in a similar fashion in the link you provided (notice how clicking on the altair legend reactively triggers code to run).

It was slightly tricky to get this working since I was expecting reactive_read(jchart, "selections") to work, but it doesn't since jchart.observe(callback, "selections") doesn't seem to fire callback when the selection changes? Kind of seems like that's oversight/issue on altair's part?

@joelostblom
Copy link
Author

This is super, thank you for the detailed example @cpsievert ! I will start experimenting with creating some Shiny + Altair dashboards on my own and see if I run into any limitations, but it looks pretty convenient from your example. I think it would be helpful to add that example to the docs and I also raised the issue you brought up in the Altair repo to see if there is something we can modify there to make the Shiny syntax more convenient.

@cpsievert cpsievert transferred this issue from posit-dev/py-shiny Nov 7, 2023
@cpsievert cpsievert changed the title Docs on Reactivity/Events from selections in charts Responding to altair chart selections Nov 16, 2023
cpsievert added a commit that referenced this issue Nov 20, 2023
@cpsievert
Copy link
Collaborator

Whoops, this was closed by mistake

@cpsievert cpsievert reopened this Nov 20, 2023
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

No branches or pull requests

2 participants