Skip to content

Responding to altair chart selections #112

Closed

Description

@joelostblom

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

Activity

  1. cpsievert commented on Oct 24, 2023

    @cpsievert
    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?

  2. joelostblom commented on Oct 25, 2023

    @joelostblom
    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.

  3. changed the title [-]Docs on Reactivity/Events from selections in charts[/-] [+]Responding to altair chart selections[/+] on Nov 16, 2023
  4. added a commit that references this issue on Nov 20, 2023
    cc51a69
  5. cpsievert commented on Nov 20, 2023

    @cpsievert
    Collaborator

    Whoops, this was closed by mistake

  6. cpsievert commented on Nov 21, 2026

    @cpsievert
    Collaborator

    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).
    • The Linked views article walks through crossfilter examples, including Altair crossfilter in-widget vs. through Shiny.

    Thanks for pushing on this back then — the gap you identified is closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions