Cannot export data to a file in R (write.csv)
Asked Answered
R

8

23

I am trying to export data in R to a csv file, and as much simple as I try to do it, I always get the same error message. Example:

I create a simple data vector to export

 x <- c(1,3,4,3,5,7,5,8,2,5,7)

I try to export with:

write.csv(x,file='whatever.csv')

And I get an error:

error in file(file ifelse (append a w )) cannot open the connection
In addition: Warning message: In file(file, ifelse(append, "a", "w")) :
cannot open  file 'whatever.csv': Permission denied

How can I solve this?

Run answered 22/7, 2013 at 2:17 Comment(3)
I do not get an error when I run your code. Seems like a possible permissions issue.Thornton
A common cause of this kind of error, especially on Windows, can be having the file open in another program like Excel while you try to write to it.Midlands
Use : write.csv(x,file='./whatever.csv')Reade
O
26

First part is to check the working directory and ensure that you have write access to that directory. You can check this with getwd(). I can reproduce your error by trying to write to a directory which is read only.

To set the working directory to something else with read access you can type setwd("H:/foo"). Once you have write access the write.csv(x,file='whatever.csv') should work.

Ostracize answered 22/7, 2013 at 2:24 Comment(1)
Yup, I got this error because I forgot to create the directory before writing the file.Paz
C
4

I got the same issue today and I know I have full permission to the folder. What worked for me is giving it the absolute path.

write.csv(data, file="C:/project/file.csv")

Calabrese answered 14/3, 2019 at 21:54 Comment(0)
S
1

If you don't specify a filename (i.e. C:/temp.csv) and just provide a file path, this same error pops up with both write.csv and write_csv.

Spandau answered 10/1, 2019 at 21:19 Comment(0)
K
0

I got this error today and fixed it by granting everyone write permission to the folder.

Steps: locate the folder on your PC, right-click and select properties, look for the "Security" tab and edit the permission for all to include "Write"

Kettle answered 2/4, 2018 at 21:18 Comment(0)
P
0

I got this error today because the file I try to rewrite on was open in another program. After I closed it, the problem solved.

Pita answered 11/7, 2020 at 10:44 Comment(0)
B
0

Related: I was trying to save a csv to a relative path, that I built incrementally in Windows, but in my case the problem wasn't an error really, but a misunderstanding on my part - in the following code:

library(dplyr)
library(hflights)

path_to_hflights_as_csv <- 
  file.path(path.expand("~/MyHomeSubDir"),
            "hflights.csv")

write.csv(hflights, path_to_hflights_as_csv)

path.expand("~/MyHomeSubDir") is mapped to "C:/Users/my.username/Documents/MyHomeSubDir" instead of "C:/Users/my.username/MyHomeSubDir".

Searching if it was some faulty config while installing R, I found that "home dir" in various Windows versions is indeed "C:/Users/my.username/Documents/" (and not "C:/Users/my.username"):

And when you pass a path including a sub directory that doesn't exist to utils::csv.write the error is similar (only the reason to not opening file is different - cannot open file 'C:/Users/my.username/MyHomeSubDir/hflights.csv': No such file or directory).

Banville answered 12/7, 2020 at 1:2 Comment(0)
C
-1

I just stumbled across this question in trying to figure it out myself. I had the exact same error message pop up a few times:

Error in file(file, ifelse(append, "a", "w")) : 
  cannot open the connection

After searching around and finding nothing that worked for me I restarted R and received the same message, but also a new error:

In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
  cannot open file 'censoredpath.file.csv': Permission denied

I went to my file explorer and attempted to open the .csv in Excel and it notified me that it was locked by another user (someone else had the file open on their computer). So if it's not a problem with having access to the directory like what's already been suggested, try opening it in Excel to see if that might be the problem.

Condolent answered 7/9, 2017 at 13:50 Comment(0)
C
-2

In case you tried everything and did not work, check your antivirus. In my case AVAST was causing this issue for some reason.

Cargo answered 7/1, 2023 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.