How to Solve R Error in file(file, “rt”) cannot open the connection

by | Programming, R, Tips

If you try to read a CSV file in R but the file name or directory that the file is in does not exist, you will raise the Error in file(file, “rt”) cannot open the connection.

If the file does exist, you can either change your working directory to location of the file using setwd() or provide the absolute path to the CSV file.

This tutorial will go through the error in detail and how to solve it with code examples.


Example

Let’s look at an example where we want to read a CSV file into a data frame using the read.csv() function.

df <- read.csv('values.csv', header=TRUE, stringsAsFactors=FALSE)

Let’s run the code to see what happens:

Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'values.csv': No such file or directory

The error occurs because the file values.csv does not exist in the current working directory.

Solution #1: Use setwd

We can solve this error by changing into the working directory where the file is. First, we can find the working directory we are in using getwd():

getwd()
"/Users/Developer"

Then we can change to the scores directory where the file is using setwd() and read the CSV into a data frame.

setwd("/Users/Developer/scores/")

df <- read.csv('values.csv', header=TRUE, stringsAsFactors=FALSE)

print(df)

Let’s run the code to see the result:

  player_ID score
1         A    10
2         B     1
3         C    20
4         D     4
5         E     2
6         F     8
7         G     5

We successfully imported the CSV into a data frame.

Solution #2: Use Absolute Path to File

We can also solve the error by passing the absolute path to the values.csv file as follows:

df <- read.csv('/Users/Developer/scores/values.csv', header=TRUE, stringsAsFactors=FALSE)
print(df)

Let’s run the code to see the result:

  player_ID score
1         A    10
2         B     1
3         C    20
4         D     4
5         E     2
6         F     8
7         G     5

We successfully imported the CSV into a data frame.

Summary

Congratulations on reading to the end of this tutorial! The error in file(file, “rt”) cannot open the connection occurs when you try to read a CSV file in R, but the file name or directory that the file is in does not exist. You can solve this error by changing to the file’s directory or providing the absolute path to the directory.


For further reading on R related errors, go to the articles: 

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨