Ayan Saraf
Ayan Saraf

Reputation: 113

How to use uploaded file for saving onto local server in R Shiny?

I want to use the file uploaded by the user and then link it to my other R script. I am unable to get access to the file right now.

UI -> fileInput("ghiFile", "Choose GHI File (.csv)", 
accept=c('text/csv',  'text/comma-separated-values','text/plain', '.csv')),

The server.r file is empty as I am unable to get access of the file. I want to save the uploaded file on my local machine for now.

Upvotes: 3

Views: 477

Answers (1)

Florian
Florian

Reputation: 25375

Like this:

ui.R

ui <- shinyUI(fluidPage(

  fileInput('target_upload', 'Choose file to upload',
            accept = c(
              'text/csv',
              'text/comma-separated-values',
              '.csv'
            )),
  DT::dataTableOutput("sample_table")

)
)

server.R

library(shiny)
library(DT)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

  df_products_upload <- reactive({
    inFile <- input$target_upload
    if (is.null(inFile))
      return(NULL)
    df <- read.csv(inFile$datapath, header = TRUE,sep = ";")
    return(df)
  })

  output$sample_table<- DT::renderDataTable({
    df <- df_products_upload()
    DT::datatable(df)
  })

}
)

Of course, you have to either make sure that the separator is correct, or also use an input for that. Hope this helps!

Upvotes: 1

Related Questions