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))
使用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就一般架构而言,我建议在目录创建方面采用以下结构。这将涵盖大多数潜在问题,目录创建的任何其他问题将由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"]
要确定路径是否为有效目录,请尝试:
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))
截至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))
此解决方案将工作目录置于用户的控制之下。在原始文章中,使用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))
}
}
然后,在需要操纵fi的任何后续脚本中