【问题标题】:How to get correct file path after uploading using fileInput using R shiny?使用 R Shiny 使用 fileInput 上传后如何获取正确的文件路径?
【发布时间】:2019-12-26 17:37:08
【问题描述】:

我有一个上传文件的代码。使用操作按钮“保存到数据库”上传文件后,我将文件名和文件路径存储在向量中。

在同一个应用程序中,我有另一个选项卡以表格形式显示 excel 输出。因此,为了读取文件,我使用在使用操作按钮保存文件时检索到的文件路径。

问题是我得到“文件不存在”,因为路径如下所示

“C:\Users\Arun\AppData\Local\Temp\RtmpINivvL/69ff834f0b2623ef2ec95c41/0.xlsx”

虽然我上传文件的位置是

" "D:/Data_Dump/summary.xlsx"

如何解决这个问题?

UI.R code

tabItem(tabName = "file",
              mainPanel(
                titlePanel(h2("Upload your XLSX file here ")), fluidRow(
                  column(6,
                         fileInput('file1', 'Choose a XLSX file to upload',
                                   accept = c('.xlsx'))),
                  column(6,actionButton("save","Save to Database")),
                  div(DT::dataTableOutput("contents"),style = "font-size: 100%;width: 150%")
                )

              )
      )

server.R code

eventReactive(input$save,{

    filenm <- input$file1
    filenm$name



    tablelist <<- c(tablelist,as.character(filenm$name))
    print(tablelist)
    filePath <<- c(filePath,as.character(filenm$datapath))
    print(filePath)

    return (tablelist)
  })

【问题讨论】:

    标签: r user-interface shiny shinydashboard


    【解决方案1】:

    获取实际文件路径的一种解决方法可能是使用shinyFiles 包。但是您必须手动通过file.copy() 之类的方式确保上传功能。此外,在shinyFiles 中,您无法指定要接受的特定文件类型。

    这里是shinyFiles的一个小例子:

    library(shiny)
    library(shinyFiles)
    
    ui <- fluidPage(
      shinyFilesButton("Btn_GetFile", "Choose a file" ,
                       title = "Please select a file:", multiple = FALSE,
                       buttonType = "default", class = NULL),
    
      textOutput("txt_file")     
    )
    
    server <- function(input,output,session){
      tablelist<-NULL
      filePath<<-NULL
      volumes = getVolumes()
      observe({  
        shinyFileChoose(input, "Btn_GetFile", roots = volumes, session = session)
    
        if(!is.null(input$Btn_GetFile)){
           browser()
          file_selected<-parseFilePaths(volumes, input$Btn_GetFile)
          #check if file extension is .xlsx 
          tablelist <<- c(tablelist,as.character(file_selected$name))
          print(tablelist)
          filePath <<- c(filePath,as.character(file_selected$datapath))
          print(filePath)
          #upload file via e.g. file.copy()
          output$txt_file <- renderText(as.character(file_selected$datapath))
        }
      })
    }
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2018-02-17
      • 1970-01-01
      • 2019-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多