无法将数据导出到r(write.csv)中的文件

发布于 2025-02-04 14:40:47 字数 472 浏览 2 评论 0 原文

我试图将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

如何解决此问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

一个人的旅程 2025-02-11 14:40:47

第一部分是检查工作目录并确保您对该目录的写入访问权限。您可以使用 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.

滿滿的愛 2025-02-11 14:40:47

我今天遇到了同样的问题,我知道我对文件夹有完全的许可。对我有用的是给它绝对的道路。

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")

各自安好 2025-02-11 14:40:47

如果您不指定文件名(即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.

甜`诱少女 2025-02-11 14:40:47

我今天遇到了这个错误,因为我尝试重写的文件是在另一个程序中打开的。关闭它后,问题解决了。

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.

听风吹 2025-02-11 14:40:47

我今天遇到了这个错误,并通过授予每个人将所有人的权限授予文件夹来修复。

步骤:在您的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"

落日海湾 2025-02-11 14:40:47

相关:我试图将CSV保存到一个相对路径,这是我在Windows中逐步构建的,但是在我的情况下,问题确实不是错误,而是对我的误解 - 在以下代码中:

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。 。用户名/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:

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).

伴我老 2025-02-11 14:40:47

我只是偶然发现了这个问题,试图自己弄清楚。我有几次完全相同的错误消息:

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

搜索并发现对我有用的任何东西都没有重新启动R并收到了相同的消息,但也是一个新的错误:

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

我去了我的文件资源管理器,并试图打开。 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:

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.

南渊 2025-02-11 14:40:47

如果您尝试了一切并且没有工作,请检查您的防病毒软件。在我的情况下,Avast出于某种原因引起了这个问题。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文