可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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.
回答2:
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"
回答3:
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.
回答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")
回答5:
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.
回答6:
Related: i was trying to save a csv to a relative path, that i built incrementaly 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"
):
- https://en.wikipedia.org/wiki/Home_directory
- https://cran.r-project.org/bin/windows/base/rw-FAQ.html#What-are-HOME-and-working-directories_003f
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
).
回答7:
If you have exported a file before (no problem) and now the error occurs, make sure the file is not open. In my case when I tried to export a second time I got the error, I realized that the first file was open. You can change the name of the file to be exported or close the one that is open.
回答8:
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.
回答9:
If you already have a file with the same name in your working directory, you would get that error.
回答10:
Please check your code about containing any variable name, logical T or F (TRUE or FALSE). If you have you can't shorten logical values as T or F.