2

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

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
an2825
  • 23
  • 1
  • 1
  • 6
  • Looks like the path is incorrect. Try removing the tilde. – YOLO Mar 04 '18 at 00:19
  • I still get the same error with these changes. `file.list <- list.files(path="C:/Users/Bob/Documents/R/natural-language-processing/class-notes", pattern=".csv")` `getwd()` is `"C:/Users/Bob/Documents/R/natural-language-processing"` – an2825 Mar 04 '18 at 00:26
  • Just to check, if this path is correct. Remove the pattern and see if you get list of files in the directory. – YOLO Mar 04 '18 at 00:36
  • Pattern removed, I still get the same list of files in the directory. – an2825 Mar 04 '18 at 00:48
  • Use the `full.names = TRUE` in `list.files` – akrun Mar 04 '18 at 01:02

5 Answers5

8

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)
Richard Border
  • 3,209
  • 16
  • 30
  • 2
    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 Mar 04 '18 at 00:50
1

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.

LMunyan
  • 116
  • 6
  • 1
    Note that this will only work if you're using Windows – Richard Border Mar 04 '18 at 00:50
  • @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. – LMunyan Mar 04 '18 at 01:26
0

I just spent a lot of time trying to understand what was wrong on my code too...

And it seems to be simple if you are using windows.

When you name your file "blabla.txt" then windows name it "blabla.txt.txt"... That's the same with .CSV files so windows create a file named "001.csv.csv" if you called it "001.csv"

So, when you create your .csv file, just rename it "001" and open it in R using read.table("/absolute/path/of/directory/with/required/001.csv")

It works for me.

0

In my case, the problem was that inside the directory with the *.csv files was also another directory. As soon as I moved the directory from the folder, the specific error disappeared.

0

If I can add something, I was having this same problem and the issue was a syncing problem with my cloud device. Saving my files on a local folder solved the problem.