As of version 0.12.0, Shiny has built-in support for interacting with static plots generated by R’s base graphics functions, and those generated by ggplot2.
This makes it easy to add features like selecting points and regions, as well as zooming in and out of images.
To get the position of the mouse when a plot is clicked, you simply need to use the
click option with the
plotOutput(). For example, this will define a new input value,
input$plot_click, which contains the location of the previous mouse click.
For example, this app will print out the x and y coordinate position of the mouse cursor when a click occurs (to see it in action, click in the plot area of the app rendered below the code):
Notice that the x and y coordinates are scaled to the data, as opposed to simply being the pixel coordinates. This makes it easy to use those values to select or filter data.
The other types of interactions are double-clicking, hovering, and brushing. (Brushing is clicking and dragging a selection box.) They can be enabled with the
brush options. In the example below, all of these are enabled, and the coordinates are displayed below
hover have x and y coordinates,
brush is slightly different: because it’s a box, it has
Next: learn about how to easily select rows of data with interactive plots.
For more on this topic, see the following resources:
If you have questions about this article or would like to discuss ideas presented here, please post on RStudio Community. Our developers monitor these forums and answer questions periodically. See help for more help with all things Shiny.