rinvalid-argument

Error when importing text from files (readChar Error in file.info(filename[i]) : invalid filename argument)


I was importing text from files following the code explained here by Tommy:

Import text file as single character string

I imported the name of the files (which are in subfolders):

mydata <- as.data.frame(list.files(path="FolderWithFiles",
                                     full.names = FALSE, recursive = 
TRUE, ignore.case= TRUE, include.dirs = TRUE))

Then I was using a loop to import the content for all the files (the working directory is set to "FolderWithFiles"):

filename <- mydata$filename

x<-(1:245)
y<-c(1:245)

for ( i in x) {

  y[i]  <- readChar(filename[i] , file.info(filename[i])$size)

}

This used to work until I changed some of the files. Now I keep getting this error and I cannot solve it:

Error in file.info(filename[i]) : invalid filename argument  

Solution

  • I found a solution, I post it for anyone experiencing the same error:

    Error in file.info(filename[i]) : invalid filename argument  
    

    For some reason after updating R, it started importing the names of files as factors rather than characters. It was enough to add:

    mydata$filename <- as.character(mydata$filename)