How to Fix the R error in file(file, ifelse(append, ‘a’, ‘w’)) : cannot open the connection

When saving data files in the R programming language you will sometimes get error messages. The “error in file(file, ifelse(append, “a”, “w”)) : cannot open the connection” error message is one that you will encounter if there is a problem with the directory you are saving to. Reduced to its most basic meaning this is a directory not found error.

Description of the error

This is a similar error to what you encounter when the computer cannot find files that it is trying to load. However, it is usually a problem with the path to the folder in which you are trying to save your data. This usually results from an incorrect path. Problems with the path to a folder can occur if your program does not end up in the directory that you think it does. In this particular case, there are three possible ways of fixing this problem, but only one of them will get you to a specific folder where you may want to save your data.

Explanation of the error

Here is an example of code that produces this error message. While there are other ways of triggering it this is a common one. It is an attempt to establish a relative path to save data to the desktop in your part of the computer. The problem is that it does not achieve the path that it is trying to make. The goal here is for anybody using this program to get their files saved to the same place from their perspective regardless of the actual path.

> t = as.numeric(Sys.time())
> set.seed(t)
> x = rnorm(10)
> x
[1] 1.4428939 0.9813567 0.4186076 -0.1544381 -0.7696074 1.0569071 -0.6557092 -1.5247600 0.5607932 0.9742717
> write(x, file = “~/Desktop/test.txt”, sep = “,”)
Error in file(file, ifelse(append, “a”, “w”)) :
cannot open the connection

In this example we are trying to save the data to a location that has the path of “C:/Users/name /Desktop/test.txt” but instead we are getting “C:/Users/name/Documents/Desktop/test.txt” for the directory. So unless you have created a folder in your Documents folder called Desktop, the program will not find the folder and it will return our error message. The key to understanding this error is that you are trying to save files to a non-existing folder.

How to fix the error

Here we have two ways of fixing this problem. The first one sends your files to the current working directory of your project. The second one while more complicated actually sends your files to the Desktop, even if somebody else is running the program.

> t = as.numeric(Sys.time())
> set.seed(t)
> x = rnorm(10)
> x
[1] 1.4428939 0.9813567 0.4186076 -0.1544381 -0.7696074 1.0569071 -0.6557092 -1.5247600 0.5607932 0.9742717
> write(x, file = “test.txt”, sep = “,”)

This is the simplest solution to this problem, and it involves simply sending your files to the current working directory. You are literally fixing the problem by simply staying with the default directory.

> t = as.numeric(Sys.time())
> set.seed(t)
> x = rnorm(10)
> x
[1] 1.4428939 0.9813567 0.4186076 -0.1544381 -0.7696074 1.0569071 -0.6557092 -1.5247600 0.5607932 0.9742717
> dr = getwd()
> dr = dirname(dr)
> dr = dirname(dr)
> dr = dirname(dr)
> dr = paste(dr, “Desktop/test.txt”, sep=”/”)
> dr
[1] “C:/Users/name/Desktop/test.txt”
> write(x, file = dr, sep = “,”)

The second fix starts with the working directory and bumps it down to its parent directory. It turns out that several rows of this function can get the directory location to where you need it. Warning if your copy of R is in a different location you may have to adjust the steps, but the process is the same. As you can see the directory that you are sending your file to is the user name’s desktop. This is exactly where we wanted to be.

This is actually a fairly easy problem to avoid because you will not get it as long as you stick to saving data to the default directory. However, there are going to be times when this is not going to be what you need to do. In such cases the second method for fixing this error, even if you have to adjust for your own situation, will be the solution to saving your data wherever you need it to be.

Scroll to top