You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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")
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?
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.
Closing this out — chart selections driving Shiny reactivity is now fully supported and documented.
The Altair guide shows exactly the pattern you asked about: declare a selection in Vega-Lite's grammar, then read it from Python reactively via @render_altair's selected() / selected_rows() accessors — no JavaScript or callbacks needed. selected_rows() even gives you the selected rows of your data frame directly.
The Reacting to widgets guide covers the general accessor pattern (plotly, bokeh, bqplot, jscatter, lonboard, and more all have selection accessors now).
Activity
cpsievert commentedon Oct 24, 2023
Yes,
{shinywidgets}'sreactive_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, thereactive_read(map, "center")line essentially does a reactive version ofmap.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 sincejchart.observe(callback, "selections")doesn't seem to firecallbackwhen the selection changes? Kind of seems like that's oversight/issue on altair's part?joelostblom commentedon Oct 25, 2023
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.
[-]Docs on Reactivity/Events from selections in charts[/-][+]Responding to altair chart selections[/+]Close #112. Avoid setting a new default height in order to get consis…
cpsievert commentedon Nov 20, 2023
Whoops, this was closed by mistake
cpsievert commentedon Nov 21, 2026
Closing this out — chart selections driving Shiny reactivity is now fully supported and documented.
@render_altair'sselected()/selected_rows()accessors — no JavaScript or callbacks needed.selected_rows()even gives you the selected rows of your data frame directly.Thanks for pushing on this back then — the gap you identified is closed.