如何将此数据帧转换为
https://docs.google.com/spreadsheets/d/1Z_qdynfqA8f95sNUPq-zPw9NNk5rqT9cV8yTUOwZXrk/edit#gid=0
嵌套的子弹?类似这样的东西:
大项目符号1
- 样本1
- 事件1
- 事件2
- 细节1
- 细节2
大子弹2
- 样本1
- 样品2
- 样品3
大子弹3
- 样本1
- 事件1
- 细节1
- 细节2
- 事件2
- 细节1
- 细节2
- 事件3
- 细节1
- 细节2
- 事件1
- 样品2
- 事件1
- 事件2
这就是主意。
我在一个情节后面有一个部分需要一个项目符号的叙述。与我共事的人不知道R,如果叙事在电子表格中,那么与他们合作(同时自动化生成(更容易。
对此我不知所措。我的尝试没有成功。PS我正在用正确的参数knit_global((寻找外部脚本
也许可以使用dplyr
来解决。给定一个列表
- A
- AA
- AA1
- AA2
- AB
- AB1
- AA
哪个将具有excel/R结构
bullet_1 bullet_2 bullet_3
1 A AA AA1
2 A AA AA2
3 A AB AB1
我们可以从底层开始迭代group_by
和summarise
(使用paste
(,并且对于通过少分组一个变量在列表中向上一级的每个连续迭代。也就是说,对于第一次迭代,group_by级别1(A(和级别2(AA,AB(。这将导致
bullet_1 bullet_2 bullet_3
1 A AA "AA2, AA1"
2 A AB AB1
二、按bullet_1
分组
bullet_1 bullet_2
1 A "AA, AA2, AA1, AB, AB1"
最后是
bullet_1
1 "A, AA, AA2, AA1, AB, AB1"
在整数之间添加一些Markdown列表输出,并在excel(NA(中处理空单元格,这种非常快速和肮脏的尝试可能是一个起点(替换read_excel
中的路径(
```{r, echo=FALSE, results='asis'}
library(dplyr)
frameLoop <- readxl::read_excel("path/to/xlsxfile.xlsx") %>%
#add newlines (n) after each entry to create list structure further down the road.
#NA's will be removed later. If NAs are converted to "", will interfere with
#list structure
mutate_all(function(x) ifelse(is.na(x), x ,paste0(x, "n"))) %>%
replace(is.na(.), "@NA@")
frameNcol <- ncol(frameLoop)
# Number of indentations (measured in number of spaces) needed to create list
# structure in markdown. Hard coded, the list can be generated for max 3 levels
numSpaces <- c(0, 2, 4)
# Vectors of column names.
# The dynamic is updated each iteration such that columns which are
# removed are not attempted to used in summarise().
bulletNamesDynamic <- bulletNamesStatic <- colnames(frameLoop)
#Counting backwards as we start at the lowest list level
for(i in frameNcol:1){
#These are the columns to group by each iteration
bulletGroups <- bulletNamesStatic[1:(i-1)]
#The name to give the new variable. In this case it's the same as the one already used
sumNameVar <- paste0("bullet_", i)
#(tentative) column names to summarise
sumVars <- rev(paste0("bullet_", frameNcol:(i)))
#However, must be adjusted each iteration due to the fact that
#for each iteration, the "final" column is removed
sumVars <- sumVars[sumVars %in% bulletNamesDynamic]
#At the final iteration, set prefix for the "header", i.e. top level list entry
if(i == 1){
prefix <- paste0("n#### ")
} else {
prefix <- paste0(paste(rep(" ", numSpaces[i-1]), collapse = ""), "- ")
}
frameLoop <- frameLoop %>%
group_by_at(bulletGroups) %>%
summarise(!!sym(sumNameVar) := paste0(prefix, !!!syms(sumVars), collapse="")) %>%
#Removes NAs. At each iteration, the summarise will not
#combine a true list entry with NA (by definition). As a consequence, all
#entries containing @NA@ will be removed. Might be problematic if true entries
#contains "@NA@", e.g. xxxx@NA@. Should be fairly easy to modify
mutate(!!sym(sumNameVar) := ifelse(grepl("@NA@", !!sym(sumNameVar)), "", !!sym(sumNameVar)))
bulletNamesDynamic <- colnames(frameLoop)
}
paste(frameLoop[[1]], collapse = "") %>%
cat()
```
输出:
#### Big Bullet 1
- Sample 1
- Event 1
- Event 2
- Detail 1
- Detail 2
#### Big Bullet 2
- Sample 1
- Sample 2
- Sample 3
#### Big Bullet 3
- Sample 1
- Event 1
- Detail 1
- Detail 2
- Event 2
- Detail 1
- Detail 2
- Event 3
- Detail 1
- Detail 2
- Sample 2
- Event 1
- Event 2