首页 > 解决方案 > 分辨率变化时如何避免重复渲染ggplot

问题描述

我在更改屏幕分辨率时遇到了 ggplot 渲染本身的问题,尤其是当我处理大文件时,这使得我的 shinyApp 每次分辨率更改时都很忙。有没有办法避免这种情况?

library(shiny)
library(ggplot2)

ui <- fluidPage(
  plotOutput("plot")
)

server <- function(input, output, session) {

  data<-mtcars
  for(i in 1:15){
    data<-rbind(data,data)
  }


  output$plot<-renderPlot({
    ggplot(aa,aes(x = cyl, y = mpg))+geom_point()
  })

}

shinyApp(ui, server)

标签: rggplot2shiny

解决方案


我想说的是在更改屏幕分辨率时(例如放大/缩小),我可以看到我们从头开始运行 renderPlot 而 renderTable dosent 这样做。您可以运行代码并在控制台中查看打印结果

library(shiny)
library(ggplot2)

ui <- fluidPage(
  plotOutput("plot"),
  tableOutput("table")
)

server <- function(input, output, session) {

  data<-mtcars
  for(i in 1:5){
    data<-rbind(data,data)
  }


  output$plot<-renderPlot({
    print("plot")
    ggplot(data,aes(x = cyl, y = mpg))+geom_point()
  })

output$table<-renderTable({
print("table")
head(data)
})
}

shinyApp(ui, server)

推荐阅读