0

This is not an specific question, but mostly a "looking for suggestions", since any kind of answer is welcome. I built a huge website using pure shiny in R, but a really small thing is bothering me..

I'm plotting some elements over an image, let's say.. rectangles (could be anything), and interact with those elements using their position on the image. One of the actions is deleting some of those elements, but -as far as my understanding of R plotting system is- that can't be done, since an element plotted in R can't be removed, so my workaround is basically "plotting everything else" but the element i just deleted.

So, my question is, is there any way to "remove" an object in a plot without having to replot everything else (basically not making a new plot with everything but the deleted element)?

Thanks in advance!

Ghost
  • 1,426
  • 5
  • 19
  • 38
  • 1
    How's the plot created? Is it created using ggplot2 or plotly or anything else? Many shiny libraries has a `proxy` object which allows you to modify the object instead of recreating it. – yusuzech Oct 15 '19 at 17:33
  • `baseplot` always, i've never used ggplot (i don't like it). If there is a good shot there i could switch to it, mind suggesting me one of these libraries to dig deeper? (Thanks a lot!) – Ghost Oct 15 '19 at 17:37
  • I don't think it's possible with base plot, since they are rendered as static images in HTML. The only option is to re-render the image. But, if you use svg based graphs. You can remove the svg elements using javascript with css selectors or proxy object provided by the package. You can reference: https://stackoverflow.com/questions/50620360/using-proxy-interface-in-plotly-shiny-to-dynamically-change-data – yusuzech Oct 15 '19 at 17:43
  • Well, i'd try the ggplot solution, i'm gonna take a look. – Ghost Oct 15 '19 at 17:48
  • I've been looking for 40 minutes and haven't been able to even find a way to plot an image in ggplot.. i don't get the ggplot mechanics; could you please point me to some examples on how to plot a simple image? – Ghost Oct 15 '19 at 18:30
  • 1
    ggplot2 uses similar mechanics as base plot(plots are rendered as static images in HTML). If your goal is to remove element without re-ploting, the only options you have would be svg based graphs and some libraries you can use are `r2d3` or `plotly`. But if you want to know how to use ggplot2, you can reference: http://r-statistics.co/ggplot2-Tutorial-With-R.html – yusuzech Oct 15 '19 at 18:48
  • [Plot caching](https://shiny.rstudio.com/articles/plot-caching.html) might be of interest. This also works with base R plots. – ismirsehregal Oct 15 '19 at 20:34
  • Already tried with plot caching, it still "replots" everything, the time it saves is in the code-executing part, but not too much in the plotting part. Can't believe there isn't a "super-modern-dynamic" plot library for R =/ – Ghost Oct 15 '19 at 20:37
  • 1
    As already mentioned by @yifyan please have a look at e.g. `library(plotly)` and it's [proxy interface](https://plot.ly/r/plotlyproxy/) to change plots without rerendering in shiny. – ismirsehregal Oct 16 '19 at 07:25
  • Will take a look at it today, thanks boys! – Ghost Oct 16 '19 at 10:52

0 Answers0