25

I created a folder in order for it to be the main work directory meaning all the files I create go there, and files I read will be from there. For some reason after I created the folder and I'm trying to set it as the working directory I get this message:

Error in setwd("~/") : cannot change working directory  

When I try to create files they are saved somewhere different and I get an error when trying to load them. I used the scan function and got:

Error in file(file, "r") : cannot open the connection

In addition: Warning message:

In file(file, "r") : cannot open file 'ff': No such file or directory
Michael Ohlrogge
  • 10,559
  • 5
  • 48
  • 76
Lee Neumann
  • 291
  • 1
  • 3
  • 3
  • I think there is an error in the path. The reason r saves files to "somewhere different" is that it uses the default working directory when it fails to change it with the `setwd()`. You can check the default working directory from Global Options. Can you tell us the path in to the intended working directory? – Olli J Nov 13 '15 at 12:10
  • 1
    What is your OS? Windows? –  Nov 13 '15 at 12:41
  • 1
    Try using the menus: Session -> Set Working Directory -> Choose Directory... and then see what the console says after you have selected the directory you want from the GUI. – Ken Benoit Nov 13 '15 at 12:45

4 Answers4

23

The command setwd("~/") should set your working directory to your home directory. You might be experiencing problems because the OS you are using does not recognise "~/" as your home directory: this might be because of the OS, or it might be because of not having set that as your home directory elsewhere.

As you have tagged the post using RStudio:

  • In the bottom right window move the tab over to 'files'.
  • Navigate through there to whichever folder you were planning to use as your working directory.
  • Under 'more' click 'set as working directory'

You will now have set the folder as your working directory. Use the command getwd() to get the working directory as it is now set, and save that as a variable string at the top of your script. Then use setwd with that string as the argument, so that each time you run the script you use the same directory.

For example at the top of my script I would have:

work_dir <- "C:/Users/john.smith/Documents"
setwd(work_dir)
JCollerton
  • 3,227
  • 2
  • 20
  • 25
  • 1
    It helped. I was trying to set the path using the `./`. When I set it using the GUI it showed in the console the full path `~/.../my_working_dir`. Thanks – dum4ll3 Nov 29 '17 at 13:49
11

This may help... use the following code and browse the folder you want to set as the working folder

setwd(choose.dir())
Dharman
  • 30,962
  • 25
  • 85
  • 135
Hadi Pourbagher
  • 151
  • 1
  • 5
  • This will work only for Windows, as choose.dir is Windows-specific function. https://stackoverflow.com/questions/33511964/r-windows-os-choose-dir-file-chooser-wont-open-at-working-directory – Nazariy Perepichka Mar 04 '20 at 14:39
2

I just had this error message happen. When searching for why, I figured out that there's a related issue that can occur if you're not paying attention - the same error occurs if the directory you are trying to move into does not exist.

David Manheim
  • 2,553
  • 2
  • 27
  • 42
  • 1
    Maybe it is the case that you have your path in couple of lines, you used enter to make it? If so, then part of you paths might look like that "/\nData/" instead of "/Data/", which causes the problem. Just set it to be in one line and issue is solved! – M_D Aug 05 '19 at 13:59
  • That would help in many cases, but my case was a simpler one of me being stupid and, IIRC, mistyping the directory name. – David Manheim Aug 06 '19 at 14:49
0

Maybe it is the case that you have your path in couple of lines, you used enter to make it? If so, then part of you paths might look like that "/\nData/" instead of "/Data/", which causes the problem. Just set it to be in one line and issue is solved!

M_D
  • 287
  • 3
  • 13