"无法更改工作目录"使用dir.create时出错

时间:2016-04-06 16:07:59

标签: r directory setwd

我使用dir.create创建了一组嵌套文件夹,现在我无法将我的工作目录设置为其中任何一个。

我已经检查了我想要更改的目录的拼写,这是正确的。它存在。我可以将文件移入其中并将文件保存在其中。

我不知道发生了什么事。非常生气。

想法?

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       

补充说这很有效:

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

这是产生错误的完整代码。

#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

1 个答案:

答案 0 :(得分:1)

尝试setwd("./new_dir")

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

使用showWarnings = FALSEmainDir是主目录,subDir是您创建的子目录。 使用showWarnings = FALSE时要小心,因为这也会隐藏其他警告,例如未创建的目录..

 # 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)