无法将数据导出到r(write.csv)中的文件
我试图将R中的数据导出到CSV文件,并且尽管我尝试这样做的简单,但我总是会收到相同的错误消息。示例:
我创建了一个简单的数据向量来导出
x <- c(1,3,4,3,5,7,5,8,2,5,7)
我尝试导出的信息:
write.csv(x,file='whatever.csv')
我会遇到一个错误:
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
如何解决此问题?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
第一部分是检查工作目录并确保您对该目录的写入访问权限。您可以使用
getwd()
检查此内容。我可以通过尝试写入仅读取的目录来重现您的错误。要将工作目录设置为使用读取访问的其他内容,您可以键入
setWD(“ h:/foo”)
。写入访问后,
write.csv(x,file ='whywen.csv')
应该有效。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.我今天遇到了同样的问题,我知道我对文件夹有完全的许可。对我有用的是给它绝对的道路。
write.csv(data,file =“ c:/project/file.csv”)
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")
如果您不指定文件名(即c:/temp.csv),而只是提供一个文件路径,则同样的错误会在write.csv和write_csv中弹出。
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.
我今天遇到了这个错误,因为我尝试重写的文件是在另一个程序中打开的。关闭它后,问题解决了。
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.
我今天遇到了这个错误,并通过授予每个人将所有人的权限授予文件夹来修复。
步骤:在您的PC上找到文件夹,右键单击并选择属性,查找“安全”选项卡并编辑权限,以使所有人都包含“写”
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"
相关:我试图将CSV保存到一个相对路径,这是我在Windows中逐步构建的,但是在我的情况下,问题确实不是错误,而是对我的误解 - 在以下代码中:
PATH。 。用户名/myhomesubdir“
。搜索安装R时是否有一些错误的配置,我发现各种Windows版本中的“ Home Dir”确实是
“ C:/users/my.username/documents/”
(而不是) “ c:/users/my.username”
):包括
utils :: csv.write
的路径,其中不存在的子目录该错误是相似的( 不打开文件的原因是不同的 /em> -无法打开文件'c:/users/my.username/myhomesubdir/hflights.csv':no uke file或Directory
)。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:
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
).我只是偶然发现了这个问题,试图自己弄清楚。我有几次完全相同的错误消息:
搜索并发现对我有用的任何东西都没有重新启动R并收到了相同的消息,但也是一个新的错误:
我去了我的文件资源管理器,并试图打开。 excel中的CSV,它通知我它是另一个用户锁定的(其他人在其计算机上打开了文件)。因此,如果访问已经建议的目录不是问题,请尝试在Excel中打开它,以查看是否可能是问题。
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:
After searching around and finding nothing that worked for me I restarted R and received the same message, but also a new error:
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.
如果您尝试了一切并且没有工作,请检查您的防病毒软件。在我的情况下,Avast出于某种原因引起了这个问题。
In case you tried everything and did not work, check your antivirus. In my case AVAST was causing this issue for some reason.