“read_excel"在闪亮的应用程序中 [英] "read_excel" in a Shiny app

查看:34
本文介绍了“read_excel"在闪亮的应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Shiny 应用程序,它使用 xlsx 包中的 read.xlsx 函数.一切正常,但我想从 readxl 更改为 read_excel,希望它能更快并能够处理大文件.

I have a Shiny app that uses the read.xlsx function from package xlsx. All works fine, but I want to change to read_excel from readxl, hoping it would be faster and able to cope with large files.

用户界面部分:

fileInput("inputFile","Upload file...")

服务器部分:

  data <- reactive({
    inFile <- input$inputFile
    if (is.null(inFile)) { return(NULL) }    
    dataFile <- read_excel(inFile$datapath,sheet=1)
    return(dataFile)
  })

我收到未知格式"错误.

I get the "Unknown format" error.

inFile$datapath 是 "/tmp/.../60974676c7287e913d1c0dc5/0"
inFile$type 是application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

inFile$datapath is "/tmp/.../60974676c7287e913d1c0dc5/0"
inFile$type is "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

问题 1:有没有办法告诉 read_excel 它是一个 xlsx 类型的文件?
问题二:上传文件的存放位置可以控制吗?

Question 1: is there a way to tell read_excel that it's a xlsx type file?
Question 2: is it possible to control the location where the uploaded file will be stored?

推荐答案

这是一个未解决的问题 与 readxl 包.当前提供的解决方法是复制文件数据路径并附加 .xlsx.这是我机器上的一个工作示例,仅限于 .xlsx 文件编辑为使用 file.rename 而不是 file.copy.

This was an open issue with the readxl package. The current workaround provided there is to copy the file data path and append .xlsx. Here is a working example on my machine limited to .xlsx files edited to use file.rename instead of file.copy.

library(shiny)
library(readxl)

runApp(
    list(
        ui = fluidPage(
            titlePanel("Use readxl"),
            sidebarLayout(
                sidebarPanel(
                    fileInput('file1', 'Choose xlsx file',
                              accept = c(".xlsx")
                              )
                    ),
                mainPanel(
                    tableOutput('contents'))
                )
            ),
        server = function(input, output){
            output$contents <- renderTable({
                inFile <- input$file1

                if(is.null(inFile))
                    return(NULL)
                file.rename(inFile$datapath,
                          paste(inFile$datapath, ".xlsx", sep=""))
                read_excel(paste(inFile$datapath, ".xlsx", sep=""), 1)
            })
        }
        )
    )

编辑请注意,使用 1.1.0 版本的 readxl 不再需要重命名文件.以下对我来说现在没有问题.

EDIT Note that with the 1.1.0 version of readxl it no longer needs to have the file renamed. The following works without a problem for me now.

library(shiny)
library(readxl)

runApp(
  list(
    ui = fluidPage(
      titlePanel("Use readxl"),
      sidebarLayout(
        sidebarPanel(
          fileInput('file1', 'Choose xlsx file',
                    accept = c(".xlsx")
          )
        ),
        mainPanel(
          tableOutput('contents'))
      )
    ),
    server = function(input, output){
      output$contents <- renderTable({

        req(input$file1)

        inFile <- input$file1

        read_excel(inFile$datapath, 1)
      })
    }
  )
)

这篇关于“read_excel"在闪亮的应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆