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?
There are two ways of exporting data into text files through R. One is using the base R functions and another one is using the functions from the readr package to export data into text/CSV format.
Note that you can export data from R to several formats, like CSV, SAV, XLS, XLSX, TXT or even XML.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With