[R] setwd in windows

Troels Ring tring at gvdnet.dk
Fri Jul 14 13:42:14 CEST 2017


Dear friends - windows R 3.3.3 - sorry to ask a simple question - but I 
cannot make setwd work properly in scripts

In the example below I have made a directory in C (firstdir) and a 
directory in that (secdir) and the intention is to change directory to 
the second from the first - it works when I put the entire path but not 
the tilde - what am I missing? I was around rwf 2.14 as per the help to 
setwd - to define the HOME - and redefine it. As I read 2.14 R_USER is 
used for HOME when set. To no avail.

Best wishes

Troels

getwd()
#[1] "C:/firstdir"
  Sys.getenv("R_USER")
#"C:\\Users\\Bruger\\Documents"
dir()
#[1] "chdir.R" "secdir"
setwd("~/secdir")
error in setwd("~/secdir") : cannot change working directory
setwd("C:/firstdir/secdir")
getwd()
#"C:/firstdir/secdir"
setwd("C:/firstdir")
Sys.setenv(R_USER = "C:/firstdir")
Sys.getenv("R_USER")
setwd("~/secdir")


      2.14 What are HOME and working directories?

Several places in the documentation use these terms.

The working directory is the directory from which|Rgui|or|Rterm|was 
launched, unless a shortcut was used when it is given by the ‘Start in’ 
field of the shortcut’s properties. You can find this from R code by the 
call|getwd()|.

The home directory is set as follows: If environment variable|R_USER|is 
set, its value is used. Otherwise if environment variable|HOME|is set, 
its value is used. After those two user-controllable settings, R tries 
to find system-defined home directories. It first tries to use the 
Windows "personal" directory (typicallyC:\Users\username\Documents). If 
that fails, if both environment variables|HOMEDRIVE|and|HOMEPATH|are set 
(and they normally are), the value is${HOMEDRIVE}${HOMEPATH}. If all of 
these fail, the current working directory is used.




	[[alternative HTML version deleted]]



More information about the R-help mailing list