【问题标题】:read excel in R shiny works slow在 R Shiny 中阅读 excel 工作缓慢
【发布时间】:2018-11-01 11:56:14
【问题描述】:

我有一个闪亮的应用程序,它应该首先从两个 excel 文件中提取/读取数据,然后使用响应式自定义 excel 文件的结果以生成两个表。问题是excel文件很重,应用程序需要花费大量时间来读取excel文件。它还减慢了反应性响应时间。下面是一个解释我的应用程序结构的虚拟代码。

    library(readxl)

    ui <- fluidPage(

    tableOutput('dt1'),
    tableOutput('dt2'),
    selectInput('choice','TLT Select',choices= c('Yes','No'))

    )

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

    t.1 a <-as.data.frame(read_excel(path ="A.xlsx") ,stringsAsFactors = FALSE) #this is a 45 MB excel file

    t.2 <- a <-as.data.frame(read_excel(path ="B.xlsx") ,stringsAsFactors = FALSE) # this is a 50 MB excel file

     excel.a <- reactive({

     if (input$choice == 'Yes') {
      subset(t.1, t.1$Action == 'Yes') }
      else 
      {subset(t.1, t.1$Action == 'No')}


     excel.b <- reactive({

     if (input$choice == 'Yes') {
      subset(t.2, t.2$Action == 'Yes') }
      else 
      {subset(t.2, t.2$Action == 'No')}


      output$dt1 <- renderTable({
      excel.1()
     })

      output$dt2 <- renderTable({
      excel.2()
     })

    })

    }

shinyApp(ui, server)

这当然不是一个可重现的示例,但这就是我的查询的结构。知道如何让它更高效、更快,从而使其加载更快,反应也更快吗?

谢谢

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    在浏览器中试用或添加“浏览”按钮。 添加到服务器类似:

     data <- reactive({
        validate(
          need(input$file != "", "Please select a data set !!!\nUse 'Browse' bottom to choose\n")
        )
    
        inFile <- input$file
        if (is.null(inFile))
          return(NULL)
        a <-as.data.frame(read_excel(inFile$datapath) ,stringsAsFactors = FALSE) 
        ....... # all manipulation
    
    # and in UI
    fileInput("file", "Choose Date file:",accept = c('.xlsx')
    

    可能这不是文件下载时间,可能是HTML css等格式的表示数据时间

    【讨论】:

    • 感谢您的帮助,这可能是一种解决方案,但是这个应用程序是为高管准备的,要求他们上传文件对他们来说确实不是一个好主意,您知道我的意思 :-)
    • 在这种情况下,我会从data.table 切换到fread 并首先将所有文件转换为.csv。这是我通常采用的方法,因为从用户体验的角度来看,read_excel 也可能更糟。
    • 您的意思是我们在 R 中使用 fread 将文件转换为 csv,还是我需要事先将 excel 文件转换为 csv?
    • 事先,虽然我认为可能有选择以这种方式直接导入 .XLSX,但不确定。但是,我不知道上下文,因此之前转换可能不适合您。
    猜你喜欢
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 2020-03-13
    相关资源
    最近更新 更多