简体   繁体   中英

“cannot change working directory” error using dir.create

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM