How to Fix in R: Error in file(file, “rt”) : cannot open the connection
In this tutorial you’ll learn how to fix the message “Error in file(file, “rt”) : cannot open the connection” in the R programming language.
The page looks as follows:
If you want to learn more about these content blocks, keep reading:
Example 1: Reproducing the Message: Error in file(file, “rt”) : cannot open the connection
The R programming code below illustrates why the error message “Error in file(file, “rt”) : cannot open the connection” appears in R.
Let’s assume that we want to import a csv file to R that is called my_data.csv. Then, we can use the read.csv function as shown below:
my_data <- read.csv("my_data.csv") # Try to import data # Error in file(file, "rt") : cannot open the connection # In addition: Warning message: # In file(file, "rt") : # cannot open file 'my_data.csv': No such file or directory
However, as you can see the RStudio console returned the error message “Error in file(file, “rt”) : cannot open the connection”. The reason for this is that we didn’t properly specify the working directory in which the csv file is stored.
The following example explains how to solve this error…
Example 2: Fixing the Message: Error in file(file, “rt”) : cannot open the connection
In this Example, I’ll illustrate how to fix the error message “Error in file(file, “rt”) : cannot open the connection”. For this, we have to specify the working directory were out csv file is stored using the setwd function:
setwd("C:/Users/Joach/Desktop/") # Change working directory
Now, we can execute exactly the same code as we did in Example 1…
my_data <- read.csv("my_data.csv") # Properly import data
…and this time it works without any error messages.
Video & Further Resources
Have a look at the following video of my YouTube channel. In the video, I’m explaining the R code of this article:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you may want to read the other articles on this homepage:
You learned in this tutorial how to handle the error message “Error in file(file, “rt”) : cannot open the connection” in the R programming language. In case you have additional questions and/or comments, please let me know in the comments section. Furthermore, don’t forget to subscribe to my email newsletter to receive updates on the newest tutorials.
28 Comments. Leave new
This didn’t work. Still same error after resetting working directory several different ways
Hey,
Could you provide the R code that you have used to specify your working directory?
Regards
Joachim
> dta_tmp setwd(“c:/Users/geraldbamundaga/Downloads/”)
Error in setwd(“c:/Users/geraldbamundaga/Downloads/”) :
cannot change working directory
Hey Gerald,
This error often appears due to spelling mistakes in the path. In your example, shouldn’t it be an upper case C at the beginning of the path?
Regards
Joachim
Hello Joachim
I am getting a similar error:
read.csv(“C:\\Marco\\work\\Excel stuff\\Luis Investigation dec 2021″)
”
Error in file(file, “rt”) : cannot open the connection
In addition: Warning message:
In file(file, “rt”) :
cannot open file ‘C:\Marco\work\Excel stuff\Luis Investigation dec 2021’: Permission denied
”
I assume that mine is a permissions issue (this is a company laptop) : I have tried running RStudio as an administrator and also changing the permissions of R through the R folder under program files. Any other ideas on what I could do?
Hey Marco,
Please have a look at this thread on Stack Overflow. It seems to solve your problem.
I hope that helps!
Joachim
Hello Joachim,
I come to you because I still get the same « connection error ». I have tried many things but I still don’t get a way to solve this problem.
Here is the script (dowloading a zip file first):
file.path=”https://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?sort=1&file=comext%2FCOMEXT_DATA%2FPRODUCTS%2Ffullxixu202111.7z”
temp=tempfile()
download.file(file.path, temp)
read.table(unz(temp, “fullxixu202111.dat”))
I think that it should work, but the read.table command always entails the connection error.
Do you have any idea on what could solve it ?
Thanks !
Hey Louu,
I’m sorry for the late response, I just took a few days off.
Do you still need help on this?
Regards,
Joachim
Hello Joachim,
Even when I change the directory I still have the same error :
setwd(“C:/Users/perri/Desktop/M1_Recherche/Semestre_7/Initiation_R”)
> read.csv(‘C:/Users/perri/Desktop/M1_Recherche/Semestre_7/Initiation_R/AIID_subsubtest.csv’)
Error in file(file, “rt”) : cannot open the connection
In addition: Warning message:
In file(file, “rt”) :
cannot open file ‘C:/Users/perri/Desktop/M1_Recherche/Semestre_7/Initiation_R/AIID_subsubtest.csv’: No such file or directory
Do you think you can help me ?
Thanks,
Perrine
Hey Perrine,
Apologies for the delayed response, I just came back from vacation. Do you still need help with this?
Regards,
Joachim
Hello Joachim,
I have this problem when i want to read a file in R. I don’t know where is the problem.
Error in file(file, “rt”) : cannot open the connection
In addition: Warning message:
In file(file, “rt”) :
cannot open file ‘Estimations parametres HW copy(1)/Estimations parametres HW copy/rates.txt’: No such file or directory
Hey Fabiola,
This error message indicates that either your file name or the path to the file is not specified correctly.
Can you change the working directory using the following code?
Regards,
Joachim
I have a same problem
> source(“/Users/ducnguyen/Documents/quan102/Quan 102 Assignments/q102-code-2022-T2.R”)
Error in file(file, “rt”) : cannot open the connection
In addition: Warning message:
In file(file, “rt”) :
cannot open file ‘expandedsurfdata.csv’: No such file or directory
> setwd(“/Users/ducnguyen/Documents/quan102/Quan 102 Assignments/q102-code-2022-T2.R”)
Error in setwd(“/Users/ducnguyen/Documents/quan102/Quan 102 Assignments/q102-code-2022-T2.R”) :
cannot change working directory
Hey Duc,
Does it work when you remove the file name from your path within the setwd() function? I.e.:
Regards,
Joachim
Hey Joachim,
I have this problem where I’m not able to change my directory
setwd(“C:/Users/VYSHAK/OneDrive/Desktop”)
Error in setwd(“C:/Users/VYSHAK/OneDrive/Desktop”) :
cannot change working directory
Hey Vyshak,
The code looks fine. Have you checked whether there are some spelling errors in your path? Note that paths are case-sensitive.
Regards,
Joachim
Hey Joachim,
I am working in RStudio. I am getting this error only when I attempt to pull up a file in an RMD code chunk. This error does not occur if I type the same command into my console.
Do you know why this might be?
Hey Lucas,
Unfortunately, I do not have much experience with RMD. I have recently created a Facebook discussion group where people can ask questions about R programming and statistics. Could you post your question there? This way, others can contribute/read as well: https://www.facebook.com/groups/statisticsglobe
Regards,
Joachim
Hello!!
i used the following codes string <-file.path("C:", "Usuarios", "esthe" , "data")
FTSEdata <- paste0(string,"/", "FTSE.100.csv")
FTSE.100.csv <- read.csv(FTSEdata, sep = "," ,
colClasses = c("character" , rep("numeric,99")),
check.names = FALSE)
FTSE.100 FTSE.100 FTSE.100 setwd(“C:/ Usuarios/ esthe/ data/ FTSE.100.CSV”)
Error in setwd(“C:/ Usuarios/ esthe/ data/ FTSE.100.CSV”) :
cannot change working directory
I have tried to move the folder, change the directories but no success, is there anything else I could do?
kind regards
Hey Esther!
It seems like you have spaces in your path and “csv” is written in capital letters. At first glance, those looked like possible issues to me.
Regards,
Cansu
> kings <- scan("https://robjhyndman.com/tsdldata/misc/kings.dat",skip=3)
Error in file(file, "r") :
cannot open the connection to 'https://robjhyndman.com/tsdldata/misc/kings.dat'
In addition: Warning message:
In file(file, "r") :
URL 'https://robjhyndman.com/tsdldata/misc/kings.dat': Timeout of 60 seconds was reached
Hey Osama,
Could you please provide some more context for your problem? What exactly are you trying to do?
Regards,
Joachim
Hey Joachim,
this is the statement
kings<-scan("https://robjhyndman.com/tsdldata/misc/kings.dat"😉
tskings<-ts(kings)
I want to get data from website and convert it to time series
thank you
It seems like you are trying to access a web page that doesn’t exist. Could you verify that the link is correct?
Regards,
Joachim
Hi Joachim,
I am working in RStudio. I am getting this error when opening the file up with another laptop.
“`{r}
df_08 <- read.csv("2008.csv")
df_07 <- read.csv("2007.csv")
plane_data <- read.csv("plane-data.csv")
“`
Error in file(file, "rt") : cannot open the connection
Quitting from lines 21-24 (R-Code-Flights-Analysis.Rmd)
Error in file(file, "rt") : cannot open the connection
Calls: … eval_with_user_handlers -> eval -> eval -> read.csv -> read.table -> file
Execution halted
Do you know how can I solve this? I have tried setwd() but had an error indicating that cannot change working directory
Hello,
It sounds strange. Maybe this page could be a starting point to solve your problem.
Regards,
Cansu
This does not help at all. After I do the setwd, the same error occurs no matter what I try.
Hello Grant,
First of all, please make sure that your folder locates in the correct folder. Then try again. If it doesn’t work, it is possible that you are not setting the working directory properly. Then setting it manually could be a good solution in your case. Please click on the Session button on the menu bar, then select Set Working Directory, and afterward, Choose Directory. Then you can find the folder in which you have the file.
Regards,
Cansu