Origins of the Error:
This is a basic 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 file at a location. R is trying to open a data file and can’t find a file to open at the location that you specified. Which explains the “cannot open the connection” portion of the error message.
You can trigger this error through any file system problem that results in a bad file reference. However, this is generally a problem with how you are referring to your working directory.
Fixing the Bug
The specifics of the error may vary slightly – R is essentially telling you it cannot find the file:
- This is most likely an issue with how you’re referring to your 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 filename directly in your code once you have set the working directory properly.
- It is possible you are having issues with relative file references to directories above and below the working directory. Remember to use “../<filereference>” for files in the parent directory.
- You could also be having issues with file suffixes. Inspect your file handles using Windows.
See our article on using setwd() to set the working directory properly.