【问题标题】:How can I create a table with names of directories and subdirectories in R?如何在 R 中创建包含目录和子目录名称的表?
【发布时间】:2016-02-12 10:26:06
【问题描述】:

问题

我想用 R 生成一个给定路径的目录名和子目录名表。

期望的输出

输出应该是一个带有两列的 data.frame 或类似的,我可以用knitr::kable 处理它以产生一个漂亮的.htmlrmarkdown

因此结果应该或多或少像这样:

|dir names            |subdir names         |
|:--------------------|:--------------------|
|                     |                     |
| DIR_1               | SUBDIR_1            |
|                     | SUBDIR_2            |
|                     | SUBDIR_3            |
| DIR_2               | SUBDIR_1            |
|                     | SUBDIR_2            |

小例子:

这是我到目前为止走了多远:

# Create directories
dir.create("DIR_1")
dir.create("DIR_2")
# Create subdirectories
dir.create("./DIR_1/SUBDIR_1")
dir.create("./DIR_1/SUBDIR_2")
dir.create("./DIR_1/SUBDIR_3")
dir.create("./DIR_2/SUBDIR_1")
dir.create("./DIR_2/SUBDIR_2")

library("knitr")
kable(list.dirs(path=".",
                recursive = TRUE, 
                full.names = FALSE),
      col.names = c("dirs & subdirs mixed"))


|dirs & subdirs mixed |
|:--------------------|
|                     |
|DIR_1                |
|DIR_1/SUBDIR_1       |
|DIR_1/SUBDIR_2       |
|DIR_1/SUBDIR_3       |
|DIR_2                |
|DIR_2/SUBDIR_1       |
|DIR_2/SUBDIR_2       |

其他问题

如何添加包含存储在每个子目录中的所有文件名的第三列?

【问题讨论】:

  • 您是只对两个级别(dir 和 subdir)感兴趣,还是对“subdirs”中带有“subsubdirs”的任意深度嵌套感兴趣?
  • 嗯,一个可以处理深度嵌套(>2 层)的解决方案会很好。
  • 使用 list.files 和参数 include.dirs=TRUE 而不是 list.dirs。然后,您可以使用 stringr::str_split_fixed(FL,"/",n) 之类的内容拆分目录,其中 FL 是文件列表,n 是嵌套目录的最大数量。
  • 获取文件名使用basename(data frame),获取目录名使用dirname()
  • @nicola 你会根据你的建议发布答案吗?

标签: r knitr r-markdown


【解决方案1】:

基于@nicola's suggestion to use list.files and str_split_fixed,以下代码生成给定目录中所有目录和文件的表:

```{r setup, echo = FALSE, results = 'hide', warning = FALSE}
library(stringr)
lapply(X = c("demo", "demo/DIR_1", "demo/DIR_2", "demo/DIR_1/SUBDIR_1", "demo/DIR_1/SUBDIR_2", "demo/DIR_1/SUBDIR_3", "demo/DIR_2/SUBDIR_1", "demo/DIR_2/SUBDIR_2"),
       FUN = dir.create)
file.create("demo/DIR_2/SUBDIR_2/file1.txt")
file.create("demo/DIR_2/SUBDIR_2/file12.txt")
```

```{r}
paths <- list.files(path = "demo/", include.dirs = TRUE, recursive = TRUE)
mytable <- str_split_fixed(paths, pattern = "/", n = str_count(paths, "/") + 1)

colnames(mytable) <- paste("Level", seq(ncol(mytable)))

knitr::kable(mytable)
```

第一个块只是创建了一些演示目录和文件。实际工作由list.filesstr_split_fixed完成。

  • 由于include.dirs = TRUErecursive = TRUElist.files 返回一个包含所有目录、子目录和文件的向量。
  • str_split_fixed/ 处拆分paths 并返回一个矩阵。由str_count(paths, "/") + 1动态确定分割点的数量。

输出:

|Level 1 |Level 2  |Level 3    |
|:-------|:--------|:----------|
|DIR_1   |         |           |
|DIR_1   |SUBDIR_1 |           |
|DIR_1   |SUBDIR_2 |           |
|DIR_1   |SUBDIR_3 |           |
|DIR_2   |         |           |
|DIR_2   |SUBDIR_1 |           |
|DIR_2   |SUBDIR_2 |           |
|DIR_2   |SUBDIR_2 |file1.txt  |
|DIR_2   |SUBDIR_2 |file12.txt |

【讨论】:

    猜你喜欢
    • 2017-11-28
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2020-06-17
    • 2021-11-28
    • 1970-01-01
    • 2010-10-20
    相关资源
    最近更新 更多