R 使用大型数据集时,闪亮应用程序的计算速度非常慢

R 使用大型数据集时,闪亮应用程序的计算速度非常慢,r,ggplot2,shiny,dplyr,R,Ggplot2,Shiny,Dplyr,目前,我正在为一个拥有大数据集(1.5 GB CSV,我将其压缩为150 MB RDS)的客户开发一款闪亮的应用程序。每次用户更改输入时,我都会遇到麻烦,似乎最慢的一步是,每次更改都会执行数据导入。下面是一个简单的例子(这个应用程序有点复杂,但问题是一样的) R(R studio中的基本示例,这里没有相关内容,只是选择输入和ggplot): R(服务器函数外的readRDS语句和简单的dplyr过滤器) 库(闪亮) 图书馆(dplyr) 图书馆(magrittr) 图书馆(GG2) 数据系统。时

目前,我正在为一个拥有大数据集(1.5 GB CSV,我将其压缩为150 MB RDS)的客户开发一款闪亮的应用程序。每次用户更改输入时,我都会遇到麻烦,似乎最慢的一步是,每次更改都会执行数据导入。下面是一个简单的例子(这个应用程序有点复杂,但问题是一样的)

R(R studio中的基本示例,这里没有相关内容,只是选择输入和ggplot):

R(服务器函数外的readRDS语句和简单的dplyr过滤器)

库(闪亮)
图书馆(dplyr)
图书馆(magrittr)
图书馆(GG2)
数据系统。时间(ggplot(过滤数据)+几何直方图(aes(X)))
用户系统运行时间
0.001   0.000   0.001 
我认为问题在于每次输入更改时都会执行数据导入语句,但我还没有找到阻止这种情况发生的方法

谢谢

  • 理想情况下,您不需要将这么大的文件加载到内存中,而是使用数据库,请查看
    rstudio
    网站上的存储选项
  • 可以改善用户交互的是使用
    debounce
    ,其中
    selectInput
    将在触发前延迟一定的时间

shinyServer(功能(输入、输出、会话){
选择
  • 理想情况下,您不需要将这么大的文件加载到内存中,而是使用数据库,请查看
    rstudio
    网站上的存储选项
  • 可以改善用户交互的是使用
    debounce
    ,其中
    selectInput
    将在触发前延迟一定的时间

shinyServer(功能(输入、输出、会话){

选择因为您在
shinyServer
函数之外运行了
readRDS
函数,所以它应该只运行一次。筛选和绘制大型数据集可能需要时间。请尝试查看花费的确切时间。筛选后剩下多少数据?可能是ggplot?请尝试使用更简单的输出。数据也可以是e在ggplot中,您只使用了X,在筛选数据时可能只保留X列,因为您在
shinyServer
函数之外运行了
readRDS
函数,该函数只应运行一次。筛选和打印大型数据集可能需要时间。请尝试查看具体花费的时间。筛选后剩余的数据量有多少ing?也许罪魁祸首是ggplot?请尝试使用更简单的输出。数据也嵌入在ggplot中,您仅使用X,在筛选数据时可能仅保留X列
library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput("select_z", "Z Value", selected = 387.5,c(seq(293.5,443.5,1)))
    ),

    # Show a plot of the generated distribution
    mainPanel(
       plotOutput("distPlot")
    )
  )
))
library(shiny)
library(dplyr)
library(magrittr)
library(ggplot2)

data <- readRDS('./data.rds')

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

  output$distPlot <- renderPlot({

    # generate bins based on input$bins from ui.R
    filtered_data <- data %>% filter(Z == input$select_z)

    # draw the histogram with the specified number of bins
    ggplot(filtered_data)+
      geom_histogram(aes(X))

  })

})
system.time(readRDS('./data.rds'))
   user  system elapsed 
  3.121   0.396   3.524 
> system.time(filtered_data <- data %>% filter(Z == 384.5))
   user  system elapsed 
  0.048   0.011   0.059 
> system.time(ggplot(filtered_data)+geom_histogram(aes(X)))
   user  system elapsed 
  0.001   0.000   0.001 
shinyServer(function(input, output,session) {

  selection <- reactive({
    input$select_z
  })

  # add a delay of 1 sec
  selected_z <- selection %>% debounce(1000)

  output$distPlot <- renderPlot({
    # generate bins based on input$bins from ui.R
    filtered_data <- data %>% filter(Z == selected_z())

    # draw the histogram with the specified number of bins
    ggplot(filtered_data)+
      geom_histogram(aes(X))
  })
})