Manoj Agrawal
Manoj Agrawal

Reputation: 815

R shiny csv or excel upload option

I have a requirement of giving users an option to upload a file in .csv/.txt or .xlsx format.

I am using xlsx package and have provided a radio button on my UI such as

ui <- dashboardPage(
  dashboardHeader(title = "SKU Health Check App"),
  dashboardSidebar(
    width = 350,
    radioButtons(
      "fileType_Input",
      label = h4("Choose File type"),
      choices = list(".csv/txt" = 1, ".xlsx" = 2),
      selected = 1,
      inline = TRUE
    ),
    fileInput(
      'file1',
      h4('Upload Items List'),
      accept = c(
        'text/csv',
        'text/comma-separated-values,text/plain',
        '.csv',
        '.xlsx'
      )
    ),

and to handle the options in server as

server <- function(input, output, session) {

  # Get the upload file
  get_item_list <- reactive({
    inFile <- input$file1

    if (is.null(inFile)) {
      return(NULL) }

    if (input$fileType_Input == 1) {
      read.csv(inFile$datapath,
               header = TRUE,
               stringsAsFactors = FALSE)
    } else {
      read.xlsx(inFile$datapath,
                header = TRUE,sheetIndex = 1,
                stringsAsFactors = FALSE)
    }
  })

But I am getting error as my file is not getting read even with option 1 which was working earlier without radio button and if condition. I am unable to debug as the debugger runs the block of code at once.

Can someone help please?

thanks,

Manoj Agrawal

Upvotes: 4

Views: 3338

Answers (1)

Manoj Agrawal
Manoj Agrawal

Reputation: 815

Aargh...I was just missing "" in the if condition so it should be

if (input$fileType_Input == "1") {
      read.csv(inFile$datapath,

Upvotes: 4

Related Questions