我经常发现自己在编写生成大量输出的R脚本。我发现把这个输出放到它自己的目录中更干净。我下面所写的内容将检查是否存在一个目录并移动到其中,或者创建一个目录然后移动到其中。有没有更好的解决办法?

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

当前回答

在通用架构方面,我推荐以下关于目录创建的结构。这将涵盖大多数潜在的问题,而与目录创建有关的任何其他问题将由dir检测到。创建调用。

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
}

还要注意,如果~/foo不存在,则调用dir.create('~/foo/bar')将失败,除非指定recursive = TRUE。

其他回答

使用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))

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

在通用架构方面,我推荐以下关于目录创建的结构。这将涵盖大多数潜在的问题,而与目录创建有关的任何其他问题将由dir检测到。创建调用。

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
}

还要注意,如果~/foo不存在,则调用dir.create('~/foo/bar')将失败,除非指定recursive = TRUE。

一行程序:

If (!dir.exists(output_dir)) {dir.create(output_dir)}

例子:

dateDIR <- as.character(Sys.Date())
outputDIR <- file.path(outD, dateDIR)
if (!dir.exists(outputDIR)) {dir.create(outputDIR)}

下面是简单的检查,如果不存在就创建dir:

## 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!")
}