问题:
我想用R创建一个给定路径的目录名和子目录名的表。
期望输出:
输出应该是data.frame或类似的,有两列,我可以使用: QuantumRenderer: shutdown
02-12 12:49:24.539 13109 13132 W System.err: java.lang.reflect.InvocationTargetException
02-12 12:49:24.540 13109 13132 W System.err: at java.lang.reflect.Method.invoke(Native Method)
02-12 12:49:24.540 13109 13132 W System.err: at javafxports.android.DalvikLauncher$1.run(DalvikLauncher.java:188)
02-12 12:49:24.540 13109 13132 W System.err: at java.lang.Thread.run(Thread.java:818)
02-12 12:49:24.540 13109 13132 W System.err: Caused by: java.lang.RuntimeException: Exception in Application start method
02-12 12:49:24.540 13109 13132 W System.err: at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
02-12 12:49:24.540 13109 13132 W System.err: at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$137(LauncherImpl.java:182)
02-12 12:49:24.540 13109 13132 W System.err: at com.sun.javafx.application.LauncherImpl.access$lambda$1(LauncherImpl.java)
02-12 12:49:24.540 13109 13132 W System.err: at com.sun.javafx.application.LauncherImpl$$Lambda$2.run(Unknown Source)
02-12 12:49:24.540 13109 13132 W System.err: ... 1 more
02-12 12:49:24.540 13109 13132 W System.err: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.net.URL.toExternalForm()' on a null object reference
02-12 12:49:24.541 13109 13132 W System.err: at com.rewetest.MyBrowser.<init>(MyBrowser.java:69)
02-12 12:49:24.541 13109 13132 W System.err: at com.rewetest.Main.start(Main.java:30)
02-12 12:49:24.541 13109 13132 W System.err: at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$144(LauncherImpl.java:863)
02-12 12:49:24.541 13109 13132 W System.err: at com.sun.javafx.application.LauncherImpl.access$lambda$8(LauncherImpl.java)
02-12 12:49:24.541 13109 13132 W System.err: at com.sun.javafx.application.LauncherImpl$$Lambda$9.run(Unknown Source)
02-12 12:49:24.541 13109 13132 W System.err: at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$157(PlatformImpl.java:326)
来处理,以生成带knitr::kable
的漂亮.html
。
因此结果应该或多或少看起来像这样:
rmarkdown
最小示例:
到目前为止我走了多远:
|dir names |subdir names |
|:--------------------|:--------------------|
| | |
| DIR_1 | SUBDIR_1 |
| | SUBDIR_2 |
| | SUBDIR_3 |
| DIR_2 | SUBDIR_1 |
| | SUBDIR_2 |
其他问题:
如何添加包含存储在每个子目录中的所有文件名的第三列?
答案 0 :(得分: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.files
和str_split_fixed
完成。
list.files
和include.dirs = TRUE
,recursive = TRUE
会返回包含所有目录,子目录和文件的向量。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 |