1

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  
Antonio
  • 158
  • 1
  • 14
  • Do you need to fix your working directory? – Mako212 Nov 03 '17 at 18:01
  • `file.info` needs a path, not just the file name, unless your working directory is set to the containing folder. – Mako212 Nov 03 '17 at 18:02
  • Sorry I didn't specify, the working directory is indeed set to the containing folder. – Antonio Nov 03 '17 at 18:08
  • Did you change actual file names, and is that what broke it? It's possible you could have invalid characters in your file names. – Mako212 Nov 03 '17 at 18:25
  • all the file names are similar to this, where the first name and the year are the subfolders: ""Section/yyyy/Journal-Argument_SubArgument-dd.mm.yyyy-comment.txt"" I just added the last part, I don't think the name is the problem. I guess – Antonio Nov 03 '17 at 18:34

1 Answers1

1

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)
Antonio
  • 158
  • 1
  • 14
  • 1
    When you create a data frame, character vectors are automatically converted to factors. You can either specify `as.data.frame(..., stringsAsFactors=FALSE)`, or you can go the tidy route and create a `tibble`. – Robert McDonald Oct 21 '19 at 12:59