[R] setwd in windows

peter dalgaard pdalgd at gmail.com
Fri Jul 14 13:54:42 CEST 2017


I am not sure what is going on here. The example seems incomplete. At any rate:

(a) I would be wary of changing my home directory mid-session. That is usually just not done.

(b) For a relative directory change, is anything wrong with setwd("./secdir") ?

-pd

> On 14 Jul 2017, at 13:42 , Troels Ring <tring at gvdnet.dk> wrote:
> 
> 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]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list