Thursday, January 07, 2016

R: 'file' must be a character string or connection

I was using Shiny app in R and I was trying to read a file using fileInput control but I faced the following error:

Listening on http://127.0.0.1:3558
Error in read.table(file = file, header = header, sep = sep, quote = quote,  :
  'file' must be a character string or connection


Problem code:

if(is.null(input$file1))
      return(NULL) 
file <- read.csv(file=input$file1,head=TRUE,sep=",", stringsAsFactors=FALSE)
csv <- str(file)

Solution:

inFile <- input$file1
if (is.null(inFile))
      return(NULL)    

csv <- read.csv(inFile$datapath, header=TRUE, sep=",")

The resolution was to use datapath property of the input file and not the file itself.

3 comments:

  1. This is a broad scope of dialects and toolboxs utilized by Data Scientists. ExcelR Data Science Courses

    ReplyDelete
  2. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.

    Digital marketing course mumbai

    ReplyDelete
  3. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
    Data science Interview Questions
    Data Science Course

    ReplyDelete