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.
Author by
an2825
Updated on January 28, 2020Comments
-
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 about 6 yearsLooks like the path is incorrect. Try removing the tilde.
-
an2825 about 6 yearsI 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"
-
YOLO about 6 yearsJust to check, if this path is correct. Remove the pattern and see if you get list of files in the directory.
-
an2825 about 6 yearsPattern removed, I still get the same list of files in the directory.
-
akrun about 6 yearsUse the
full.names = TRUE
inlist.files
-
-
Richard Border about 6 yearsAlso, as a minor suggestion, setting
header=TRUE
is unnecessary when usingread.csv
, this is the default setting (in contrast toread.table
). -
Richard Border about 6 yearsNote that this will only work if you're using Windows
-
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.