将基本ggplot缓存在闪亮的应用程序中,并允许动态修改图层(与ggplot等效的leafletProxy)

xcl*_*tet 6 r ggplot2 shiny

如果显示的基本数据集很大(在下面的示例工作代码中),则在闪亮的应用程序中的ggplot中添加图层或从ggplot中删除图层可能需要一段时间。

问题是:

有没有一种方法可以缓存 ggplot(基本图)并添加/删除/修改额外的(动态)图层,而无需在闪亮的应用程序中重做整个图?也就是说,该功能等效于传单地图的leafletProxy()(请参阅传单Rstudio Web页面中的工作示例)。

在此stackoverflow线程中提出了一种可能的解决方法(以下示例中的选项B),但是,它不能防止ggplot重做整个图。

工作代码示例:

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

  })
)
Run Code Online (Sandbox Code Playgroud)

JD *_*ell 0

您始终可以只制作两个图,然后使用条件来选择要显示的图。初始渲染速度很慢,但初始化后效果很好。

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

  })
)
Run Code Online (Sandbox Code Playgroud)