error in file(file, “rt”) : cannot open the connection

Errors are just a fact of life in the world of R programming. You’re writing a simple command in your R script, playing with a data set, but some missing value or variable always comes along to mess up your script- giving you a friendly little output shaped like a warning message. The R programming language can be full of those pesky errors, but with the right script knowledge and a handy command or two, you can get your R session back on track in no time!

Origins of the Error:

This is a basic r file access error. The part of the error message “error in file(file, “rt”)” indicates the failure is with the low level function used to open a specific excel file or r code file at a location. R is trying to open a data file and can’t find a file to open at the folder location that you specified. Which explains the “cannot open the connection” portion of the error message.

You can trigger this error through any R code file system problem that results in a bad file path reference. However, this is generally a problem with how you are referring to your working directory.

Fixing the Bug

The specifics of this R programming error may vary slightly depending on what R version or R package you are using, what exactly the missing value is, and the exact command you used – but your R code is essentially telling you it cannot find the file:

  • This is most likely an issue with how you’re referring to your current working directory. Use the setwd() function to clarify where you expect the working directory to be fore that procedure. You could be dealing with an old default setting in your local environment. Specify the exact file name directly in your R code once you have set the working directory properly.
  • It is possible you are having issues with relative R file references to directories above and below the current working directory. Remember to use “../” for files in the parent directory.
  • You could also be having issues with data file suffixes. Inspect your exact file handles using Windows.

See our article on using setwd() to set the working directory properly.

Error Message: error in file(file, “rt”) : cannot open the connection
Scroll to top