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.


10 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

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