read_xlsx 无法访问 Shiny 上传的 tmp 路径

问题描述

我正在尝试编写一个 Shiny 应用程序,它将多个 xlsx 文件作为输入,处理它们并在之后提供下载摘要文件。在第一步中,我构建了一个虚拟 DF 以将文件附加到。请注意,这是我第一次使用 Shiny 应用程序。

不幸的是,应用上传文件后直接崩溃。 read_xlsx 似乎无法处理上传的 tmp 文件的路径。运行应用程序时收到以下警告/错误

Warnung in read_fun(path = enc2native(normalizePath(path)),sheet_i = sheet,NAs durch Umwandlung erzeugt
Warnung: Error in :: NA/NaN Argument

在最后的示例代码中(示例设计为仅上传 1 个文件),我添加了一些打印语句来调试警告。请参阅下面的输出。引起我注意的是第三个打印语句的 character(0) 结果,它与应用程序的警告相关。我很确定我在这里遗漏了一些非常基本的东西,但它是什么?这是否与所讨论的环境有关 here

[1] "C:\\Users\\userxyz\\AppData\\Local\\Temp\\RtmpE5aM7A/0448481d282a5ba2c08583e0/0.xlsx"
[1] 25
character(0)
character(0)
[1] "B15:Z16"

最小示例:

library(shiny)
library(readxl)
library(dplyr)

calc <- function(tmp_files,num_of_cols) {

    print(tmp_files)
    print(num_of_cols)
    
    files <- list.files(enc2native(normalizePath(tmp_files)),full.names = T)
    files2 <- list.files(tmp_files,full.names = T)
    print(files)
    print(files2

    cell_range <- paste0("B15:",toupper(letters[num_of_cols+1]),"16")
    
    print(cell_range)
    
    cols <- read_xlsx(tmp_files,range = cell_range,col_names = letters[1:cell_range]) %>%
        mutate_all(str_replace_na,replacement = "") %>%
        transmute_all(str_c,collapse = "") %>%
        slice(-1) %>%
        as.character()

}

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            fileInput("upload","Upload Files",multiple = TRUE),numericInput(inputId = "num_of_cols",label = "Specify number of Columns",value = 0),actionButton(inputId = "gobutton","GO!"),downloadButton("downloadbutton","Download Results")
        ),mainPanel(
            tableOutput("upload_overview")
        )
    )
)

server <- function(input,output) {
    
    output$upload_overview <- renderTable(input$upload$datapath)
    
    observeEvent(
        input$gobutton,{
        data <- calc(input$upload$datapath,input$num_of_cols)
    })

    output$downloadbutton <- downloadHandler(
        filename = "table.xlsx",content = function(file) {write_xlsx(data,path = file)}
    )
        
}

shinyApp(ui = ui,server = server)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)