Why are permissions denied writing this file from R?

3.4k views Asked by At

I am trying to export some data from R to .csv. It was working yesterday but today I got this error message:

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 'C:/Users/cgarf/Documents/OTL_Model': Permission denied

I recently changed java versions. I'm not sure if that could be causing the issue or if I'm missing something

1

There are 1 answers

0
r2evans On

That error is often because you are trying to write a file to a directory.

file.exists("quux")
# [1] FALSE
dir.create("quux")
file.exists("quux")
# [1] TRUE
dir.exists("quux")
# [1] TRUE
write.table(mtcars, "quux")
# Warning in file(file, ifelse(append, "a", "w")) :
#   cannot open file 'quux': Permission denied
# Error in file(file, ifelse(append, "a", "w")) : 
#   cannot open the connection

BTW, in case it helps you narrow down where the directory is being used (instead of a filename), I just greped R source (base libraries) and found exactly two instances of ifelse(append, "a", "w"):

(Those links are provided "now" as of commit 5b2f6e4.)