On 14/07/2017 7:42 AM, Troels Ring 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.

The tilde is a Unix shell convention, it's not supported by Windows. R emulates it, but you should expect some differences (as there are between Unix flavours).

To see what "~/secdir" is interpreted as, call path.expand().

On my system with Windows 10 in a VM:

> path.expand("~/secdir")
[1] "C:\\Users\\murdoch\\Documents/secdir"
> Sys.setenv(R_USER = "C:/firstdir")
> path.expand("~/secdir")
[1] "C:\\Users\\murdoch\\Documents/secdir"

R sets the home directory during startup, it doesn't consult the R_USER variable every time it needs it.

Duncan Murdoch



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


______________________________________________
R-help@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.

Reply via email to