首页 > 解决方案 > 在 R 闪亮的作品中阅读 excel 很慢

问题描述

我有一个闪亮的应用程序,它应该首先从两个 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)

这当然不是一个可重现的示例,但这就是我的查询的结构。任何想法如何使它更高效和更快,以便它快速加载并且反应响应也更快?

谢谢

标签: rshiny

解决方案


在浏览器中尝试或添加“浏览”按钮。添加到服务器,例如:

 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 等格式的表示数据的时间


推荐阅读