Is there a function or some other way to enable free-hand drawing (i.e., drawing of random shapes/sizes) using the mouse in Shiny?
Specifically, I'd like to be able to "interact" with a plot from renderPlot
by marking it in various (but non-uniform) ways. -- In other words, I want to be able to mark-up already existing graphics.
The shortcomings of functions I have found include:
- Tools for drawing points, lines, rectangles, or circles are not flexible enough for me.
- Tools are not always compatible with a
click_plot
interaction kind of set-up.
Small example with an
iframe
usingdraw.io
online toolsHere's an idea using
shinyjs
and Signature Pad, adapting the demo for "drawing over an image".shinyjs
to run the JavaScript function when the page loads. Read about usingshinyjs::extendShinyjs
here. Note from the vignette that packageV8
should be installed.CSS
App
Using only basic
shiny
functionnalities, you can build an app where you can draw manual shapes upon a simple plot. I use the baseplot
function here so it reacts quicker. It uses both click and hover parameters of theplotOutput
. If you want to do it on a more complex, preexisting plot, you might prefer ggplot to better manage the different layers? You can also think of adding a spline smoother to the points. Visual:Code of the app (a live version is accessible HERE, it's actually using the code to feed a neural network for handwritten digits recognition):
Hope it helps.. Late note: I have another question on this subject, to allow compatibility of this code with smartphone movements. See here.