Why I get this error writing data to a file

后端 未结 5 2025
孤城傲影
孤城傲影 2020-12-03 21:31

I have this code

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


        
相关标签:
5条回答
  • 2020-12-03 21:39

    Sometimes the problem is in the naming of the file. For example,I have encountered this issue when in the name of the file there was as "\", as there was a dynamic list with names. You can pass by such thing by using something like: sometext = gsub("/"," ", sometext).

    0 讨论(0)
  • 2020-12-03 21:43

    I just shared this answer with a bit of a better explanation here, but the gist of it is:

    Try opening the file in Excel to see if it's locked by another user. I was receiving the same error messages and was able to figure out that a colleague had the file open on their computer which had locked me out of the ability to edit it.

    0 讨论(0)
  • 2020-12-03 21:46

    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!

    0 讨论(0)
  • 2020-12-03 21:48

    just adding to answers here.

    The reason I was facing this error was, the path I was trying to save in exceded 256 characters, and hence the error.

    Problem was sorted once I reduced the path size.

    0 讨论(0)
  • 2020-12-03 21:55

    It happens when you open the myfile.txt and run the code. Try to close the myfile.txt in your machine and run the command. It solves your problem.

    0 讨论(0)
提交回复
热议问题