I've found the following strange behaviour R (RStudio) which has been confirmed by another user in RGui. Inside a script I want to set two variables: default.wd = getwd() tmp.wd = setwd(choose.dir()) After choosing tmp.wd the value of default.wd is shown in Workspace, but getwd() is giving back the correct string of tmp.wd. Is there a workaround for the problem? I'm working on RStudio 0.96.228 and R 2.15.2. Regards Markus
Strange behaviour of setwd/getwd
8 messages · Duncan Murdoch, Rui Barradas, Berend Hasselman +2 more
On 02/11/2012 12:57 PM, Markus Holotta wrote:
I've found the following strange behaviour R (RStudio) which has been confirmed by another user in RGui. Inside a script I want to set two variables: default.wd = getwd() tmp.wd = setwd(choose.dir()) After choosing tmp.wd the value of default.wd is shown in Workspace, but getwd() is giving back the correct string of tmp.wd. Is there a workaround for the problem?
It's not clear what the problem is from your post. As the help page says, both default.wd and tmp.wd should be the same after executing those two lines. If you want to store both the old and new directories, you should do it like this: old.wd <- setwd(choose.dir()) new.wd <- getwd() Duncan Murdoch
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121102/83ff944f/attachment.pl>
Hello, Just to be complete, in my first answer I forgot to mention your 'default.wd'. There's no need to call getwd() to do what you want. tmp.wd <- choose.dir() default.wd <- setwd(tmp.wd) Hope this helps, Rui Barradas Em 02-11-2012 16:57, Markus Holotta escreveu:
I've found the following strange behaviour R (RStudio) which has been confirmed by another user in RGui. Inside a script I want to set two variables: default.wd = getwd() tmp.wd = setwd(choose.dir()) After choosing tmp.wd the value of default.wd is shown in Workspace, but getwd() is giving back the correct string of tmp.wd. Is there a workaround for the problem? I'm working on RStudio 0.96.228 and R 2.15.2. Regards Markus
______________________________________________ R-help at r-project.org mailing list 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.
Maybe my question was not clear enough: default.wd is the working dir defined in the RStudio options. In my script I want to change it temporarily to another directory by setwd(choose.dir()) and set it back to the default.wd before calling another script. But after choosing the new directory tmp.wd shows the same path in the workspace as default.wd. Calling getwd() shows the correct path. Hope its clearer now. Am 02.11.2012, 19:14 Uhr, schrieb Duncan Murdoch <murdoch.duncan at gmail.com>:
On 02/11/2012 12:57 PM, Markus Holotta wrote:
I've found the following strange behaviour R (RStudio) which has been confirmed by another user in RGui. Inside a script I want to set two variables: default.wd = getwd() tmp.wd = setwd(choose.dir()) After choosing tmp.wd the value of default.wd is shown in Workspace, but getwd() is giving back the correct string of tmp.wd. Is there a workaround for the problem?
It's not clear what the problem is from your post. As the help page says, both default.wd and tmp.wd should be the same after executing those two lines. If you want to store both the old and new directories, you should do it like this: old.wd <- setwd(choose.dir()) new.wd <- getwd() Duncan Murdoch
On 02-11-2012, at 21:02, Markus Holotta wrote:
Maybe my question was not clear enough: default.wd is the working dir defined in the RStudio options. In my script I want to change it temporarily to another directory by setwd(choose.dir()) and set it back to the default.wd before calling another script. But after choosing the new directory tmp.wd shows the same path in the workspace as default.wd. Calling getwd() shows the correct path. Hope its clearer now.
No. Read the help for setwd(). setwd returns the current directory before the change, invisibly and with the same conventions as getwd. It will give an error if it does not succeed (including if it is not implemented). The essential bit is "before the change". Berend
Am 02.11.2012, 19:14 Uhr, schrieb Duncan Murdoch <murdoch.duncan at gmail.com>:
On 02/11/2012 12:57 PM, Markus Holotta wrote:
I've found the following strange behaviour R (RStudio) which has been confirmed by another user in RGui. Inside a script I want to set two variables: default.wd = getwd() tmp.wd = setwd(choose.dir()) After choosing tmp.wd the value of default.wd is shown in Workspace, but getwd() is giving back the correct string of tmp.wd. Is there a workaround for the problem?
It's not clear what the problem is from your post. As the help page says, both default.wd and tmp.wd should be the same after executing those two lines. If you want to store both the old and new directories, you should do it like this: old.wd <- setwd(choose.dir()) new.wd <- getwd() Duncan Murdoch
______________________________________________ R-help at r-project.org mailing list 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.
Aren't you just looking for this?
default.wd <- setwd(tmp.wd <- choose.dir())
---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of Berend Hasselman Sent: Friday, November 02, 2012 3:36 PM To: Markus Holotta Cc: r-help at r-project.org Subject: Re: [R] Strange behaviour of setwd/getwd On 02-11-2012, at 21:02, Markus Holotta wrote:
Maybe my question was not clear enough: default.wd is the working dir defined in the RStudio options. In my script I want to change it temporarily to another directory by
setwd(choose.dir()) and set it back to the default.wd before calling another script. But after choosing the new directory tmp.wd shows the same path in the workspace as default.wd. Calling getwd() shows the correct path.
Hope its clearer now.
No. Read the help for setwd(). setwd returns the current directory before the change, invisibly and with the same conventions as getwd. It will give an error if it does not succeed (including if it is not implemented). The essential bit is "before the change". Berend
Am 02.11.2012, 19:14 Uhr, schrieb Duncan Murdoch
<murdoch.duncan at gmail.com>:
On 02/11/2012 12:57 PM, Markus Holotta wrote:
I've found the following strange behaviour R (RStudio) which has
been
confirmed by another user in RGui. Inside a script I want to set two variables: default.wd = getwd() tmp.wd = setwd(choose.dir()) After choosing tmp.wd the value of default.wd is shown in
Workspace, but
getwd() is giving back the correct string of tmp.wd. Is there a workaround for the problem?
It's not clear what the problem is from your post. As the help page
says, both default.wd and tmp.wd should be the same after executing those two lines. If you want to store both the old and new directories, you should do it like this:
old.wd <- setwd(choose.dir()) new.wd <- getwd() Duncan Murdoch
______________________________________________ R-help at r-project.org mailing list 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 at r-project.org mailing list 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.
2 days later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121105/af49af11/attachment.pl>