7

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)
      })
    })

  })
)
Community
  • 1
  • 1
xclotet
  • 381
  • 2
  • 11

1 Answers1

0

You could always just make two plots and then use a conditional to pick which one to show. Initial render is slow, but it works pretty well after initialization.

library(shiny)
library(ggplot2)

shinyApp(
  shinyUI(
    fluidPage(
      sidebarLayout(
        sidebarPanel(
          checkboxInput("line", "Add line", value = TRUE)
          ),
        mainPanel(
          conditionalPanel(condition = 'input.line == false',
                           plotOutput("plot1"),
                           ),
          conditionalPanel(condition = 'input.line == true',
                           plotOutput("plot2"),
                           ),
          )
        )
      )
    ),
  shinyServer(function(input, output, session) {
    #data(diamonds)


    # Option C -------
    output$plot1 <- renderPlot({
      ggplot(diamonds, aes(x=carat, y=depth)) + geom_point()
    })
    output$plot2 <- renderPlot({
      lineData <- data.frame(x=c(1, 4), y = c(60, 75))

      ggplot(diamonds, aes(x=carat, y=depth)) + 
        geom_point()+
        geom_line(data = lineData, aes(x=x, y=y), color = "red")
    })

  })
)
JD Caddell
  • 383
  • 2
  • 10