I often find myself writing R scripts that generate a lot of output. I find it cleaner to put this output into it's own directory(s). What I've written below will check for the existence of a directory and move into it, or create the directory and then move into it. Is there a better way to approach this?

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

}

推荐答案

Use showWarnings = FALSE:

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

dir.create() does not crash if the directory already exists, it just prints out a warning. So if you can live with seeing warnings, there is no problem with just doing this:

dir.create(file.path(mainDir, subDir))
setwd(file.path(mainDir, subDir))

R相关问答推荐

为什么以及如何修复Mapview不显示所有点并且st_buffer合并一些区域R?

如何使用geom_sf在边界显示两种 colored颜色 ?

如果行和列名以相同的开头,将矩阵值设置为0

通过使用str_detect对具有相似字符串的组进行分组

用相同方法得到不同函数的ROC最优截断值

如何直接从Fortran到R的数组大小?

如何计算多个日期是否在一个日期范围内

使用across,starts_with和ifelse语句变更多个变量

移除仪表板Quarto中顶盖和车身之间的白色区域

无法定义沿边轨迹的 colored颜色 渐变(与值无关)

仅在R中的数据集开始和结束时删除所有 Select 列的具有NA的行

按组计算列中1出现的间隔年数

如何计算增加10米(0.01公里)的行?

有没有办法通过str_Detect()或其他字符串匹配函数来连接两个长度不等的数据帧?

避免在图例中显示VLINS组

如何移动点以使它们的打印不重叠

如果满足条件,则替换列的前一个值和后续值

将仪表板中的值框大小更改为Quarto

在使用ggplot2的情况下,如何在使用coord_trans函数的同时,根据未转换的坐标比来定位geom_瓷砖?

根据向量对列表元素进行排序