0

I've created a set of nested folders using dir.create, and now I cannot set my working directory to any of them.

I've checked the spelling of the directory I wish to change to, and it is correct. It exists. I can move files into it and save files within it.

I have no idea what is going on. Extremely annoyed.

Ideas?

Output of sessionInfo()

R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  
LC_CTYPE=English_UnitedStates.1252 
LC_MONETARY=English_UnitedStates.1252 
LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_2.1.0 reshape_0.8.5 r4ss_1.24.0  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.3        lattice_0.20-33    corpcor_1.6.8
gtools_3.5.0       bitops_1.0-6       grid_3.2.3
plyr_1.8.3         gtable_0.2.0      
 [9] scales_0.4.0       coda_0.18-1        KernSmooth_2.23-15
gplots_2.17.0      gdata_2.17.0       tools_3.2.3
pso_1.0.3          munsell_0.4.3     
[17] maps_3.0.2         colorspace_1.2-6   caTools_1.17.1
tcltk_3.2.3       

Adding that this worked:

dir.create("new_dir")
list.files(pattern = "new_dir")
[1] "new_dir"
setwd("new_dir")

This is the full code that produces the error.

#set working directory
setwd("C:/Users/elizabeth.councill/Desktop/Projects/Rsimulator_recdevs
/SIMS/BR/")

dir_current = getwd()

#Create directories; Do not repeat these lines if the directories 
already exist. Modify filenames as appropriate
dir.create("lognormal-lognormal/")
dir.create("gamma-lognormal/")
dir.create("lognormal-gamma/")
dir.create("gamma-gamma/")

#Create simulation directories and copy source files to each of them.
filestocopy <- c("source/BLK_WA_ctl.ss",
                 "source/BLK_WA_dat.ss",
                 "source/forecast.ss",
                 "source/ss3.exe",
             "source/starter.ss")

# Create sequence of folder names to make compact loop.
SEQ_sims <- c("lognormal-lognormal/","gamma-lognormal/","lognormal-
gamma/","gamma-gamma/")

# Create directories and move original source files over.
for (i in seq_along(SEQ_sims))
{
  for (j in 1:N)
  {
  dir.create(paste(SEQ_sims[i],j))
  file.copy(from = filestocopy,to=paste(SEQ_sims[i],j))
  }
}
setwd("C:/Users/elizabeth.councill/Desktop/Projects/Rsimulator_recdevs
/SIMS/BR/lognormal-lognormal/1")

Error in setwd("C:/Users/elizabeth.councill/Desktop/Projects
  /Rsimulator_recdevs/SIMS/BR/lognormal-lognormal/1") : 
  cannot change working directory
7
  • Little more info? sessionInfo()? Are you on Windows? If so are you running R as an administrator? Does it only happen with new, nested directories? Can you reproduce your problem in a new R session? Is this on a share drive that might have weird permissions? Commented Apr 6, 2016 at 16:23
  • I can't run as administrator. Our IT department doesn't allow users to be admins (ridiculous, but just the way it is). It only happens with folders created by dir.create. If I create a folder manually, it's not a problem. I will edit original post to display session info output. Commented Apr 6, 2016 at 16:27
  • This would be a lot more compelling of a question if you pasted in what happens when you start a new R session and run the commands dir.create("new_dir"), list.files(pattern = "new_dir"), setwd("new_dir"), including output, warnings and error messages. Commented Apr 6, 2016 at 16:31
  • Will add to post. This worked. Commented Apr 6, 2016 at 16:33
  • 1
    Added full script. Commented Apr 6, 2016 at 16:52

1 Answer 1

1

Try setwd("./new_dir")

  dir.create(file.path(mainDir, subDir), showWarnings = FALSE)
  setwd(file.path(mainDir, subDir))

Use showWarnings = FALSE, mainDir is the main directory and subDir is the subdirectory created by you. Be careful when using showWarnings = FALSE as this will also hide other warnings such as the directory not created..

 # Example:
 dir.create("SSSSS")
 dir.create("./SSSSS/xxyyzz")
 setwd("./SSSSS/xxyyzz")
 getwd()
 [1] "C:/Users/Asus/Documents/SSSSS/xxyyzz"
 sessionInfo()
 R version 3.2.3 (2015-12-10)
 Platform: x86_64-w64-mingw32/x64 (64-bit)
 Running under: Windows 10 x64 (build 10586)
Sign up to request clarification or add additional context in comments.

8 Comments

This worked! Would you please explain the usage of ./ versus / and why this worked while using / did not and/or point me to a resource that can explain this?
@ELC: Specifying ./ means you are considering the current working directory in path and then proceeding with the sub-directories you created within that. So always start with getwd(). So when you say setwd("new_dir"), this means that a directory named "new_dir" is created in whatever is your current working directory. To check your current working directory use getwd() to set new_dir as your current working directory, you have to use current path + new_dir meaning setwd("./new_dir"). Hope this helped. Note: I am talking about windows machine.
Yes, this is very helpful. Thanks!
...not quite right. On non-Windows systems ~/ is your HOME directory, not the current directory. You can think of ~/ as being equivalent to C:/Users/Your_user_name/. On all systems . is the current location, so ./something refers to something in the current location - whether or not that location is the "Home".
The home directory depends on your operating system and it's configuration. The working directory only pertains to R. The only real relation is that, by default, when you start a new R session your working directory should be your home directory. You can change your working directory freely within R.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.