22

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
888
  • 3,246
  • 8
  • 41
  • 60
  • 3
    What is the result of `file.exists("C:/mypath")` ? – dickoa Jun 17 '13 at 20:48
  • @dickoa I get Error in eval(expr, envir, enclos) : could not find function "file.exist" – 888 Jun 17 '13 at 20:52
  • My bad there's a "s" at the end. I edited my comment – dickoa Jun 17 '13 at 20:53
  • 3
    OK I see it looks like "C:/mypath" doesn't exist. Make sure to put an existing path, check carefully your path. – dickoa Jun 17 '13 at 20:57
  • @dickoa I copied and pasted the path, then I replaced "\" with "/"... The path exists, the file not yet: is it a problem? – 888 Jun 17 '13 at 20:59
  • No if the path exist even if the file doesn't exist it will work. So now `file.exists("your_path")` return `TRUE` ? – dickoa Jun 17 '13 at 21:02
  • 4
    May be you don't have permission to write in the "C:/". I use linux for years now so I don't remember very well. In that case, try to open R with administrator right or write your file elsewhere (Desktop ?) – dickoa Jun 17 '13 at 21:13
  • @dickoa This **is** an answer! It worked! (maybe I should consider moving to linux too!) – 888 Jun 17 '13 at 21:15
  • Glad it worked. Linux is great and I hope that you'll give a try in the future :) – dickoa Jun 17 '13 at 21:17
  • Also, you can change permissions in C:/ if you want to be able to write there. I don't have a Windows machine, but I'm pretty sure it's just right click something and a check box for read/write. Then reopen R. – Hana Jun 17 '13 at 22:40
  • I know it is strange, but how about `C:\\...` :-) – rlegendi Jun 18 '13 at 01:05
  • try to `setwd("C:/mypath/myfile.txt")` – vkalit Oct 16 '16 at 22:11

6 Answers6

27

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!

Maurice Reeves
  • 1,572
  • 13
  • 19
  • 3
    Ooor use ` \\\` like `C:\\Users\\username`. For all those lazy guys out there who like copy-paste as I do, just copy the path from explorer into console after running `readline()`, which will escape the backslash for you and you can copy that out from console output. – Tino Nov 16 '17 at 16:28
4

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.

mastershefi
  • 153
  • 12
1

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.

cparmstrong
  • 799
  • 6
  • 23
1

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

Ana Maria
  • 11
  • 1
1

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.

Arun kumar mahesh
  • 2,289
  • 2
  • 14
  • 22
  • 1
    This was my issue! I had a previous version open on my computer at the same time. Once I closed the file, it wrote fine. Thank you @Arun kumar mahesh! – KNN Apr 28 '22 at 21:40
0

Just another possibility. I have encountered this problem when running the following code. Because I have successfully run this code before this starting RStudio, I run successfully after restarting RStudio this time. So, sometimes, restarting will solve the problem, although I haven't figured out what happened behind.

DT::datatable(
  dt.cmbn,
  extensions = c('ColReorder','FixedColumns'), rownames = FALSE,
  options = list(
    colReorder = TRUE,
    #dom = 'Bfrtip', 
    #buttons = I('colvis'),
    scrollX = TRUE,
    fixedColumns = TRUE
    )
  )
pengchy
  • 732
  • 2
  • 14
  • 26