我有一份类似这样的文件:
```{r, include = F}
require(data.table)
dt <- data.table(
A = c(1, 4, 1, 4, 2, 4, 8),
B = c(2, 2, 3, 4, 3, 1, 5)
)
```
```{r, echo = F}
col <- "A"
knitr::asis_output(paste0("### Column ", col, "\n"))
knitr::asis_output("Counts\n")
dt[, .(n = .N), by = col]
knitr::asis_output("Statistics\n")
summary(dt[, get(col)])
```
```{r, echo = F}
col <- "B"
knitr::asis_output(paste0("### Column ", col, "\n"))
knitr::asis_output("Counts\n")
dt[, .(n = .N), by = col]
knitr::asis_output("Statistics\n")
summary(dt[, get(col)])
```
虽然这很好,但很明显,这是非常重复的。我尝试使用循环而不是复制粘贴,如下所示:
```{r, echo = F}
for (col in c("A", "B")) {
knitr::asis_output(paste0("### Column ", col, "\n"))
knitr::asis_output("Counts\n")
dt[, .(n = .N), by = col]
knitr::asis_output("Statistics\n")
summary(dt[, get(col)])
}
```
然而,这根本不打印输出。如何在RMarkdown文档中循环并生成markdown和R输出?
答案 0 :(得分:1)
print
结果需要summary
声明。您的代码将更改为以下只有print
语句添加到summary
输出的内容:
```{r, echo = F}
for (col in c("A", "B")) {
print(knitr::asis_output(paste0("### Column ", col)))
print(knitr::asis_output("Counts"))
print(dt[, .(n = .N), by = col])
print(knitr::asis_output("Statistics"))
print(summary(dt[, get(col)]))
}
```
这会产生以下结果:
## [1] "### Column A"
## attr(,"class")
## [1] "knit_asis"
## attr(,"knit_cacheable")
## [1] NA
## [1] "Counts"
## attr(,"class")
## [1] "knit_asis"
## attr(,"knit_cacheable")
## [1] NA
## A n
## 1: 1 2
## 2: 4 3
## 3: 2 1
## 4: 8 1
## [1] "Statistics"
## attr(,"class")
## [1] "knit_asis"
## attr(,"knit_cacheable")
## [1] NA
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 1.500 4.000 3.429 4.000 8.000
## [1] "### Column B"
## attr(,"class")
## [1] "knit_asis"
## attr(,"knit_cacheable")
## [1] NA
## [1] "Counts"
## attr(,"class")
## [1] "knit_asis"
## attr(,"knit_cacheable")
## [1] NA
## B n
## 1: 2 2
## 2: 3 2
## 3: 4 1
## 4: 1 1
## 5: 5 1
## [1] "Statistics"
## attr(,"class")
## [1] "knit_asis"
## attr(,"knit_cacheable")
## [1] NA
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 2.000 3.000 2.857 3.500 5.000
您可能希望在打印时重新格式化并删除属性。
修改强>
根据@ user2554330的建议,这是一个使用knitr::knit_print
的示例:
```{r, echo = F}
for (col in c("A", "B")) {
knitr::knit_print(paste0("### Column ", col))
knitr::knit_print("Counts")
knitr::knit_print(dt[, .(n = .N), by = col])
knitr::knit_print("Statistics")
knitr::knit_print(summary(dt[, get(col)]))
}
```
,它提供以下输出
## [1] "### Column A"
## [1] "Counts"
## A n
## 1: 1 2
## 2: 4 3
## 3: 2 1
## 4: 8 1
## [1] "Statistics"
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 1.500 4.000 3.429 4.000 8.000
## [1] "### Column B"
## [1] "Counts"
## B n
## 1: 2 2
## 2: 3 2
## 3: 4 1
## 4: 1 1
## 5: 5 1
## [1] "Statistics"
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 2.000 3.000 2.857 3.500 5.000
修改2
以下替代解决方案将更密切地印刷SO所要求的内容。它全局删除“##”前缀并将其添加回data.frame
(即仅在需要的地方)。它还使用cat
代替knitr::knit_print
以避免打印数组索引(即[1]
)。
```{r, echo = F, comment=NA}
for (col in c("A", "B")) {
cat(paste0("Column ", col), "\n")
cat("Counts\n")
## Add comment prefix
tdf <- as.data.frame(dt[, .(n = .N), by = col]) ## Convert to data.frame for printing
rownames(tdf) <- paste("## ", 1:nrow(tdf)) ## Add comments for printing.
knitr::knit_print(tdf)
cat("Statistics\n")
## Add comment prefix
knitr::knit_print(summary(dt[, get(col)]))
}
```
以下输出更多是SO的要求(根据对此帖的评论)。它在data.frame
输出前面加上“##”,但摘要输出没有“##”前缀;如果需要,可以添加。
Column A
Counts
A n
## 1 1 2
## 2 4 3
## 3 2 1
## 4 8 1
Statistics
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.000 1.500 4.000 3.429 4.000 8.000
Column B
Counts
B n
## 1 2 2
## 2 3 2
## 3 4 1
## 4 1 1
## 5 5 1
Statistics
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.000 2.000 3.000 2.857 3.500 5.000