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.
C:/path/to/my/directory
C:/Users/username/
directory.All credit to @dickoa again for his answer in first.
Best of luck!
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With