Adding/removing layers to/from a ggplot in a shiny app may take a while if the base data set shown is big (example working code below).
The question is:
Is there a way to cache a ggplot (the base plot) and add/remove/modify extra (dynamic) layers without redoing the whole plot in a shiny app? That is, a function equivalent to leafletProxy() for leaflet maps (see a working example in leaflet Rstudio webpage).
A possible workaround is proposed in this stackoverflow thread (Option B in the example below), however, it does not prevent ggplot from redoing the whole plot.
Example working code:
library(shiny)
library(ggplot2)
shinyApp(
shinyUI(
fluidPage(
sidebarLayout(
sidebarPanel(
checkboxInput("line", "Add line")
),
mainPanel(
plotOutput("plot")
)
)
)
),
shinyServer(function(input, output, session) {
data(diamonds)
vals <- reactiveValues(pdata=ggplot())
observeEvent(input$line, {
p <- ggplot(diamonds, aes(x=carat, y=depth)) + geom_point()
if (input$line){
lineData <- data.frame(x=c(1, 4), y = c(60, 75))
p <- p + geom_line(data = lineData, aes(x=x, y=y), color = "red")
}
vals$pdata <- p
})
# Option A ------
# output$plot <- renderPlot({
# vals$pdata
# })
#
# Option B ------
observeEvent(vals$pdata,{
output$plot <- renderPlot({
isolate(vals$pdata)
})
})
})
)