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.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

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.

 

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.


18 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

Leave a Reply

Your email address will not be published.

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

Menu
Top