R 检查目录是否存在,如果不存在,则创建';不存在
我经常发现自己编写的R脚本生成了大量的输出。我发现将这个输出放在它自己的目录中更干净。我在下面写的内容将检查目录是否存在并移到其中,或者创建目录然后移到其中。有没有更好的办法R 检查目录是否存在,如果不存在,则创建';不存在,r,R,我经常发现自己编写的R脚本生成了大量的输出。我发现将这个输出放在它自己的目录中更干净。我在下面写的内容将检查目录是否存在并移到其中,或者创建目录然后移到其中。有没有更好的办法 mainDir <- "c:/path/to/main/dir" subDir <- "outputDirectory" if (file.exists(subDir)){ setwd(file.path(mainDir, subDir)) } else { dir.create(file.p
mainDir <- "c:/path/to/main/dir"
subDir <- "outputDirectory"
if (file.exists(subDir)){
setwd(file.path(mainDir, subDir))
} else {
dir.create(file.path(mainDir, subDir))
setwd(file.path(mainDir, subDir))
}
mainDir使用showWarnings=FALSE
:
dir.create(file.path(mainDir, subDir), showWarnings = FALSE)
setwd(file.path(mainDir, subDir))
dir.create()。因此,如果您能够忍受看到警告,那么只需这样做就没有问题:
dir.create(file.path(mainDir, subDir))
setwd(file.path(mainDir, subDir))
在一般架构方面,我建议在目录创建方面采用以下结构。这将涵盖大多数潜在问题,目录创建的任何其他问题将由dir.create
调用检测到
mainDir <- "~"
subDir <- "outputDirectory"
if (file.exists(paste(mainDir, subDir, "/", sep = "/", collapse = "/"))) {
cat("subDir exists in mainDir and is a directory")
} else if (file.exists(paste(mainDir, subDir, sep = "/", collapse = "/"))) {
cat("subDir exists in mainDir but is a file")
# you will probably want to handle this separately
} else {
cat("subDir does not exist in mainDir - creating")
dir.create(file.path(mainDir, subDir))
}
if (file.exists(paste(mainDir, subDir, "/", sep = "/", collapse = "/"))) {
# By this point, the directory either existed or has been successfully created
setwd(file.path(mainDir, subDir))
} else {
cat("subDir does not exist")
# Handle this error as appropriate
}
mainDir要确定路径是否为有效目录,请尝试:
file.info(cacheDir)[1,"isdir"]
file.info
不关心末尾的斜杠
Windows上的file.exists
对于以斜杠结尾的目录,将失败,如果没有斜杠,则成功。因此,这不能用于确定路径是否为目录
file.exists("R:/data/CCAM/CCAMC160b_echam5_A2-ct-uf.-5t05N.190to240E_level1000/cache/")
[1] FALSE
file.exists("R:/data/CCAM/CCAMC160b_echam5_A2-ct-uf.-5t05N.190to240E_level1000/cache")
[1] TRUE
file.info(cacheDir)["isdir"]
截至2015年4月16日,随着R 3.2.0的发布,有一个新函数名为dir.exists()
。要使用此功能并在目录不存在时创建目录,可以使用:
ifelse(!dir.exists(file.path(mainDir, subDir)), dir.create(file.path(mainDir, subDir)), FALSE)
如果目录已存在或不可创建,则返回FALSE
;如果目录不存在但已成功创建,则返回TRUE
注意,要简单地检查目录是否存在,您可以使用
dir.exists(file.path(mainDir, subDir))
在原始文章中,使用file.exists()测试目录是否存在是一个问题。如果subDir包含现有文件的名称(而不仅仅是路径),file.exists()将返回TRUE,但对setwd()的调用将失败,因为无法将工作目录设置为指向文件
我建议使用file_test(op=“-d”,subDir),如果subDir是现有目录,则返回“TRUE”;如果subDir是现有文件或不存在的文件或目录,则返回“FALSE”。类似地,可以使用op=“-f”完成文件检查
此外,如另一条注释所述,工作目录是R环境的一部分,应该由用户控制,而不是由脚本控制。理想情况下,脚本不应该改变R环境。为了解决这个问题,我可能会使用options()来存储一个全局可用的目录,在那里我需要所有的输出
这样,考虑下面的解决方案,其中某个UNIGETAG只是一个程序员为选项名称定义的前缀,这使得不可能有同名的选项存在。(例如,如果您正在开发一个名为“filer”的包,那么可以使用filer.mainDir和filer.subDir)
以下代码将用于设置稍后在其他脚本中使用的选项(从而避免在脚本中使用setwd()),并在必要时创建文件夹:
mainDir = "c:/path/to/main/dir"
subDir = "outputDirectory"
options(someUniqueTag.mainDir = mainDir)
options(someUniqueTag.subDir = "subDir")
if (!file_test("-d", file.path(mainDir, subDir)){
if(file_test("-f", file.path(mainDir, subDir)) {
stop("Path can't be created because a file with that name already exists.")
} else {
dir.create(file.path(mainDir, subDir))
}
}
然后,在需要在subDir中操作文件的任何后续脚本中,您可以使用以下内容:
mainDir = getOption(someUniqueTag.mainDir)
subDir = getOption(someUniqueTag.subDir)
filename = "fileToBeCreated.txt"
file.create(file.path(mainDir, subDir, filename))
此解决方案将工作目录置于用户的控制之下。我在R2.15.3中遇到了一个问题,在尝试在共享网络驱动器上递归创建树结构时,我会得到一个权限错误
为了避免这种奇怪,我手动创建了结构
mkdirs <- function(fp) {
if(!file.exists(fp)) {
mkdirs(dirname(fp))
dir.create(fp)
}
}
mkdirs("H:/foo/bar")
mkdirs这里是简单检查,如果目录不存在,则创建目录:
## Provide the dir name(i.e sub dir) that you want to create under main dir:
output_dir <- file.path(main_dir, sub_dir)
if (!dir.exists(output_dir)){
dir.create(output_dir)
} else {
print("Dir already exists!")
}
##提供要在主目录下创建的目录名称(即sub-dir):
输出方向一行:
如果(!dir.存在(output_dir)){dir.create(output_dir)}
例如:
dateDIR <- as.character(Sys.Date())
outputDIR <- file.path(outD, dateDIR)
if (!dir.exists(outputDIR)) {dir.create(outputDIR)}
dateDIR我知道这个问题不久前就被问到了,但是如果有用的话,这里的软件包对于不必引用特定的文件路径和使代码更易于移植非常有用。它会自动将您的工作目录定义为.Rproj
文件所在的目录,因此,无需定义工作目录的文件路径,以下内容通常就足够了:
library(here)
if (!dir.exists(here(outputDir))) {dir.create(here(outputDir))}
我肯定看到过一个R函数,它用随机生成的名称创建一个临时目录并返回名称。我认为有一个类似的方法可以创建临时文件。我不能马上找到它们,但是Databel包()有一个函数get_temporary_file_name。你永远不应该在R代码中使用setwd()
——它基本上否定了使用工作目录的想法,因为你再也不能轻松地在计算机之间移动代码了。@hadley有趣的话题值得思考,我希望你能想到其他同样的方法。在工作中,所有计算机都同步到同一网络,因此文件路径是一致的。如果不是,我们要处理的问题比脚本的可移植性更大。在这个特别的例子中,我正在写一个脚本,它将被加载到一台机器上,这台机器将在我们的国家公园内运行2年。此脚本将从本地SQL实例获取数据,进行一些处理,并输出.csv。最终产品将是一个.bat
文件,最终用户永远不需要修改它。@Marek-ahh,我明白了。你是说我应该用类似于write.table(file=“path/to/output/directory”,…)的东西来替换对setwd()
的调用?是的。或者参数化out\u dir使用showWarnings=FALSE
时请注意,这也会隐藏其他警告,例如目录不可创建。^是否有方法仅抑制一个特定的警告?您好,我想创建嵌套目录,就像我在文件夹test1中,然后在它里面test2在它里面test3。。。但现在我面临着一个问题。有没有一种方法可以在directory1不存在的情况下创建3级目录???@PraveenKesani这就是您要寻找的:dir.create(“test1/test2/test3/”,递归