Getting error in file(file, "rt"): cannot open the connection

25,123

Solution 1

Most likely you are trying to open files from the working directory instead of the directory in which you called list.files. Instead try

D1 <- do.call("rbind",
              lapply(paste0("~/R/natural-language-processing/class-notes/",
                            file.list),
                     read.csv, header = TRUE, stringsAsFactors = FALSE))

Alternatively, you can set the full.names argument to TRUE in list.files to get complete paths:

file.list <- list.files(path="~/R/natural-language-processing/class-notes", 
                        pattern=".csv", full.names = TRUE)

Solution 2

read.csv is looking for the file names in your working directory. By changing your working directory to "C:/Users/Bob/Documents/R/natural-language-processing/class-notes", your code should work just fine.

Code:

setwd("C:/Users/Bob/Documents/R/natural-language-processing/class-notes")

Then re-run your code.

Share:
25,123
an2825
Author by

an2825

Updated on January 28, 2020

Comments

  • an2825
    an2825 over 4 years

    I am running the following code...

    #Create a list of all the files
    file.list <- list.files(path="~/R/natural-language-processing/class-notes", pattern=".csv")
    
    #Loop over file list importing them and binding them together
    D1 <- do.call("rbind",lapply(file.list, read.csv, header = TRUE, stringsAsFactors = FALSE))
    

    This is the error I get when I run do.call line above.

    Error in file(file, "rt") : cannot open the connection

    I've tried resetting my wd. My current getwd() is

    ~/R/natural-language-processing
    

    I've looked through the other

    Error in file(file, “rt”): cannot open connection

    • YOLO
      YOLO about 6 years
      Looks like the path is incorrect. Try removing the tilde.
    • an2825
      an2825 about 6 years
      I still get the same error with these changes. file.list <- list.files(path="C:/Users/Bob/Documents/R/natural-language-p‌​rocessing/class-note‌​s", pattern=".csv") getwd() is "C:/Users/Bob/Documents/R/natural-language-processing"
    • YOLO
      YOLO about 6 years
      Just to check, if this path is correct. Remove the pattern and see if you get list of files in the directory.
    • an2825
      an2825 about 6 years
      Pattern removed, I still get the same list of files in the directory.
    • akrun
      akrun about 6 years
      Use the full.names = TRUE in list.files
  • Richard Border
    Richard Border about 6 years
    Also, as a minor suggestion, setting header=TRUE is unnecessary when using read.csv, this is the default setting (in contrast to read.table).
  • Richard Border
    Richard Border about 6 years
    Note that this will only work if you're using Windows
  • LMunyan
    LMunyan about 6 years
    @an2825 - What is your output when you run getwd()? Perhaps try setting your working directory manually by clicking on Session -> Set Working Directory -> Choose Directory and then selecting the correct folder.