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
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
Hello, There's nothing strange about documented behavior. From the help page ?setwd, section Value: |setwd| returns the current directory before the change, [...]. The workaround is to call choose.dir() first and then setwd() using its return value: tmp.wd <- choose.dir() 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@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.[[alternative HTML version deleted]]
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.