“read_excel” in a Shiny app

前端 未结 2 1722
温柔的废话
温柔的废话 2021-02-19 03:57

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 readx

2条回答
  •  你的背包
    2021-02-19 04:37

    to ensure that the user does upload an .xlsx file, or you need to check the extension you're self to switch between read functions. You can extract the extension as following:

    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)
    
                ext <- tools::file_ext(inFile$name)
                file.rename(inFile$datapath,
                   paste(inFile$datapath, ext, sep="."))
                read_excel(paste(inFile$datapath, ext, sep="."), 1)
             })
            }
        )
    )
    

提交回复
热议问题