为什么我得到这个错误写入数据到一个文件 [英] Why I get this error writing data to a file

查看:689
本文介绍了为什么我得到这个错误写入数据到一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

  myvector <-c(3.45235,1.32525,...,2.41351)#有些数字
write(myvector,C:/mypath/myfile.txt)#我使用/而不是\

和我得到以下错误:


文件错误(file,ifelse(append, (file,ifelse(append,
a,w)):无法打开文件' C:/mypath/myfile.txt':没有这样的文件
或目录

我读了本教程,但我不明白什么是我的错码。任何想法?
$ b

编辑

正如@dickoa指出的,我需要一个现有的写文件的路径,所以我试图用下面的方法来简化:

$ $ $ $ $ $ $ $ file.exists(C:/ )
write(myvector,C:/myfile.txt)

令人惊讶的是:P的路径C:/存在(结果为TRUE),但我得到一个类似的错误:


文件错误(file,ifelse(append,a,w)):无法打开
连接
另外:警告消息:在文件(file,ifelse(append,
a,w)):无法打开文件'C:/mypath/myfile.txt':Permission denied


解决方案

我知道@dickoa在评论中回答了这个问题,但为了在这里提供至少一个答案,我想在Windows上通过几个简单的问题。 >


  1. 当您使用Windows时,您仍然需要使用正斜杠s的路径。在R中,反斜杠被保留用于转义值。因此,R中的路径如下所示:
    C:/ path / to / my / directory
  2. Windows,C:\受到用户帐户的写入保护。如果要写入C:\,则必须是管理员。您可以通过右键单击Windows中的R图标并选择以管理员身份运行来完成此操作。这也应该在你安装软件包的时候完成。如果您不以管理员身份运行,您可能无权在某些Windows版本上安装软件包。
  3. 如果您不想以管理员身份运行R,并且想要写入文件,则默认情况下,您将拥有 C:/ Users / username> 目录。

>

祝你好运

I have this code

myvector <- c(3.45235, 1.32525, ... , 2.41351)    # some numbers
write(myvector, "C:/mypath/myfile.txt")           # I use "/" instead of "\"

and I get the following 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 'C:/mypath/myfile.txt' : No such file or directory

I read this tutorial, but I can't understant what's wrong with my code. Any idea?

edit:

As @dickoa pointed out, I need an existing path to write a file, so I tried to simplify in the following way:

file.exists("C:/")
write(myvector, "C:/myfile.txt")

Surprisingly :P the path "C:/" exists (the result is TRUE) but I get a similar 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 'C:/mypath/myfile.txt' : Permission denied

解决方案

I know @dickoa answered the question in the comments, but in order to provide at least one answer here, I wanted to go through a few simple gotchas with R on Windows.

  1. When you are using Windows, you still have to use forward slashes for paths. In R, backslashes are reserved for escaping values. So a path in R looks like: C:/path/to/my/directory
  2. In newer variants of Windows, the C:\ is protected from writes by user accounts. If you want to write to the C:\, you must be an administrator. You can accomplish this by right-clicking on the R icon in Windows and choosing "Run as an administrator." This should also be done when you're installing packages. You may not have rights to install packages on certain Windows versions if you don't run it as an administrator.
  3. If you don't want to run R as an administrator, and you want to write to files, you will by default have rights to the C:/Users/username/ directory.

All credit to @dickoa again for his answer in first.

Best of luck!

这篇关于为什么我得到这个错误写入数据到一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆