Search code examples
javascriptrshinyvegavega-lite

Are selections in Vega visualizations accessible from outside Vega?


I've been reading about "selectors" in Vega, which allow a user to click and/or drag across a visualization to highlight data points.

From the documentation above, and searching here and elsewhere, though, I'm having trouble understanding whether selectors are accessible outside of a visualization that Vega produces. Using, for example, R and Shiny, I would like to be able to allow a user to select data points in a visualization, and for Vega to somehow publish the selected indices for use by the app (for example, to allow changing a table of data to just display information about the selected points).

The following can be pasted into the Vega example editor to produce a scatterplot with click-and-drag selection turned on:

{"description":"","data":{"values":[{"V1":15,"V2":14.9611},{"V1":-21.3411,"V2":-7.8442},{"V1":-16.7692,"V2":-5.663},{"V1":13.339,"V2":10.1382},{"V1":13.0422,"V2":19.1265}]},"mark":"point","encoding":{"x":{"field":"V2","type":"quantitative"},"y":{"field":"V1","type":"quantitative"}},"config":{"cell":{"width":500,"height":300},"mark":{"shape":"circle"}},"embed":{"renderer":"svg","actions":{"export":true,"source":false,"editor":false}},"selection":{"pts":{"type":"interval"}}}

Is it possible for Vega to expose which datapoints have been selected? Whether by writing to a hidden DOM element, exposing a javascript variable, or some other means?


Solution

  • The answer is yes but it's not very clean right now. You have to inspect the datasets and signals and reverse engineer the selections from that. A proper API is on the roadmap: https://github.com/vega/vega-lite/issues/1830.