Home > database >  Why must a create an intermediary object to reference an xlsx file in R?
Why must a create an intermediary object to reference an xlsx file in R?

Time:12-17

Why is inFile1 <- input$file1 in the code below necessary for the code to work?

Why can't I just reference the datapath directly in read_excel(inFile1$datapath, 1) ?


ui <- fluidPage(

fileInput('file1', 'Upload Data',accept = c(".xlsx")

)



server <- function(input, output){
 
  output$outcome <- renderDataTable({
    
    inFile1 <- input$file1
    
    read_excel(inFile1$datapath, 1)
  })

}

CodePudding user response:

This struck me as weird initially but fileInput provides other useful information, beyond where to read the temporary copy (datapath). From the docs:

name The filename provided by the web browser. This is not the path to read to get at the actual data that was uploaded (see datapath column).

size The size of the uploaded data, in bytes.

type The MIME type reported by the browser (for example, text/plain), or empty string if the browser didn't know.

datapath The path to a temp file that contains the data that was uploaded. This file may be deleted if the user performs another upload operation.

Useful info for an application to have on the file.

As @stefan points out, the one-liner, input$file1$datapath, is to just access that data.frame item directly.

  • Related