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:
We love it when R users help each other, but RStudio does not monitor or answer the comments in this thread. If you'd like to get specific help, we recommend the RStudio Community as well as the Shiny Discussion Forum for in depth discussion of Shiny related questions and How to get help article for a list of the best ways to get help with R code.comments powered by Disqus