【问题标题】:How to get a list of sheet names from an .xlsx file uploaded to a Shiny app using fileInput?如何使用 fileInput 从上传到 Shiny 应用程序的 .xlsx 文件中获取工作表名称列表?
【发布时间】:2021-08-19 12:08:05
【问题描述】:

我有一个包含多张工作表的 .xlsx 文件,我正在使用 ID 为“file”的 fileInput 将其上传到我的 Shiny 应用程序。我的目标是使用字符串检测加载工作表,也就是说,如果我有 3 张以随机顺序命名的工作表,名为“apple”、“orange”和“banana”,我想使用字符串匹配加载“apple”工作表工作表列表。到目前为止,当我尝试使用 excel_sheets 使用 readxl 包提取工作表名称时,我一直遇到错误,因此我无法获取工作表列表-

Warning: Error in : `path` does not exist: ‘C:\Users\AppData\Local\Temp\Rtmp6dWPYS/0b16b05aa5a58cc1d1261369/0.xlsx’

相关服务器代码如下-

    sheet_names <- reactive({
        if (!is.null(input$file)) {
            return(excel_sheets(path = input$file))
        } else {
            return(NULL)
        }
    })


apple_data <- reactive({
        req(input$file)
        inFile <- input$file
        if(is.null(inFile))
            return(NULL)
        file.rename(inFile$datapath,
                    paste(inFile$datapath, ".xlsx", sep=""))
        p<-read_excel(paste(inFile$datapath, ".xlsx", sep=""), 
    sheet = sheet_names() [str_detect(sheet_names(), regex("(apple)"))]
        

    })

在调整了各种功能后,我最终找到了一种使用 openxlsx 的方法。在下面分享解决方案-

wb<- reactive({
        req(input$file)
        inFile<- input$file
        wb<-loadWorkbook(paste(inFile$datapath, ".xlsx", sep=""))
    })
    sheetnames <- reactive({
        req(input$file)
        if (is.null(input$file)) 
            return(NULL)
        
        sheet_names<-wb()$sheet_names
        })

apple_data <- reactive({
        req(input$file)
        inFile <- input$file
        if(is.null(inFile))
            return(NULL)
        file.rename(inFile$datapath,
                    paste(inFile$datapath, ".xlsx", sep=""))
        p<-read_excel(paste(inFile$datapath, ".xlsx", sep=""), 
    sheet = sheet_names() [str_detect(sheet_names(), regex("(apple)"))]
  })

【问题讨论】:

    标签: r shiny rshiny


    【解决方案1】:

    OP 代码中的问题是函数sheet_names() 使用逻辑向量str_detect(sheet_names(), regex("(apple)")) 作为输入。相反,它会是

    ...
    sheet = sheet_names()[str_detect(sheet_names(), regex("(apple)"))])
    ...
    

    【讨论】:

    • 嘿,我已经投了票,但我没有足够的声望来反映它。感谢您的帮助!
    • 是的,这是可以理解的。 :)
    • @Nil_07 我在那里发表了评论
    • @Nil_07 对你有帮助吗?
    • 已经完成了!如果我得到更多这样有趣的用例,我将使用线程进行通信!
    【解决方案2】:

    你可以试试这个代码 -

    library(shiny)
    library(readxl)
    
    ui <- fluidPage(
      fileInput('file', 'Input'),
      tableOutput('table')
    )
    
    server <- function(input, output) {
      apple_data <- reactive({
        req(input$file)
        inFile <- input$file
        if(is.null(inFile))     return(NULL)
        sheetnames <- excel_sheets(path = inFile$datapath)
        read_excel(inFile$datapath, sheet = grep('apple', sheetnames, value = TRUE))
      })
      output$table <- renderTable(apple_data())
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • inFile$datapath 只会将位置提供给临时文件位置,而不是实际文件位置。查看here 了解原因。
    • 确实如此,但您实际上不需要实际的文件位置来读取文件。临时文件位置足以读取文件,因此inFile$datapath 有效。我刚刚在一个示例 excel 文件上测试了我编辑的答案,它可以工作。你现在可以检查一下吗?
    • 它适用于 renderTable,但由于某种原因,该文件没有为 renderDataTable 加载。另外我不太明白为什么列标题有“。”当我们使用 read_excel 时代替空格。例如,“fruit 1”将变为“fruit.1”。
    • 在我的应用程序中集成它也会产生以下错误 - 错误:path 不存在:'C:\Users\ABC\AppData\Local\Temp\RtmpuYW6Cb/b9698217e1e5475119f6db49/ 0.xlsx'
    • 好吧,R 不喜欢其中有空格的列名,因此它用 "." 替换它们。在我的代码中,我有两次sheet =。也许这就是导致您出错的原因?您可以在更新的答案中对此进行测试吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 2019-12-26
    • 2017-03-28
    相关资源
    最近更新 更多