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:

 

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


28 Comments. Leave new

  • This didn’t work. Still same error after resetting working directory several different ways

    Reply
  • > dta_tmp setwd(“c:/Users/geraldbamundaga/Downloads/”)
    Error in setwd(“c:/Users/geraldbamundaga/Downloads/”) :
    cannot change working directory

    Reply
    • 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

      Reply
  • 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?

    Reply
  • 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 !

    Reply
  • 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

    Reply
  • 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

    Reply
    • 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?

      setwd("Estimations parametres HW copy(1)/Estimations parametres HW copy")

      Regards,
      Joachim

      Reply
  • 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

    Reply
    • Hey Duc,

      Does it work when you remove the file name from your path within the setwd() function? I.e.:

      setwd("/Users/ducnguyen/Documents/quan102/Quan 102 Assignments/")

      Regards,
      Joachim

      Reply
  • 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

    Reply
    • 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

      Reply
  • 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?

    Reply
  • 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

    Reply
  • > kings <- scan("https://robjhyndman.com/tsdldata/misc/kings.dat&quot;,skip=3)
    Error in file(file, "r") :
    cannot open the connection to 'https://robjhyndman.com/tsdldata/misc/kings.dat&#039;
    In addition: Warning message:
    In file(file, "r") :
    URL 'https://robjhyndman.com/tsdldata/misc/kings.dat&#039;: Timeout of 60 seconds was reached

    Reply
  • 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

    Reply
  • Grant Waddell
    March 30, 2023 12:57 am

    This does not help at all. After I do the setwd, the same error occurs no matter what I try.

    Reply
    • 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

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top