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]]
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
Fine - no nothing is wrong with setwd("./secdir") but I got the other code with the tilde as a supplementum and wondered why I couldn't make it work. I believe R_USER was successfully renamed - and per the faq thought it should work as specified. Small problem though Troels Den 14-07-2017 kl. 13:54 skrev peter dalgaard:> 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.
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 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. >