[R] Difficulties with setting working directory

Duncan Murdoch murdoch.duncan at gmail.com
Sat Oct 15 12:20:03 CEST 2016


On 14/10/2016 2:52 PM, Anže Dejak wrote:
> Greetings,
>
> Firstly, I'd hope this is the correct e-mail address to ask about specific
> problems refering to problems with R. If it isn't, I kindly ask of you to
> re-direct me to the correct contact person.
>
> So, the thing with my version of R (I'm using RStudio for R x64 3.3.0 in,
> currently, Windows 7) is, that I'm able to set the working directory
> temporary (through using setwd() function), but once I try to set it
> permanently (through "Session -> Set Working Directory -> Choose
> Directory..."), this error appears: Error in setwd("~/...") : cannot change
> working directory.
>
> As I've seen online on certain forums, the problem, supposedly, is that R
> in Windows is unable to recognize the ~ sign. But without it, I'm unable to
> set the working directory for more than a session.

That explanation is incorrect.  R in Windows simulates a Unix shell 
interpretation of ~ .  See ?path.expand for an explanation, and run

path.expand("~")

to see how it is currently being expanded for you.

However, the fact that R interprets ~ doesn't mean it is always possible 
to use setwd() to go there.

It's also possible that RStudio has a bug, and is trying to change to a 
nonexistent directory before displaying choices to you, assuming you're 
getting the error before being offered the dialog to pick a directory.
If that's the case, you might be able to avoid the problem by asking R 
to make the change:

setwd(choose.dir())

or

setwd("a/specific/directory")

Duncan Murdoch



More information about the R-help mailing list