为什么在 rmarkdown 中循环时数据表不打印?

Why does datatable not print when looping in rmarkdown?

我正在创建动态 rmarkdown 文档。最终结果应该为数据中的每个 'classification' 创建一个选项卡。每个选项卡都应该有一个来自 DT 包的数据表,其中包含打印的数据。下面是我一直在使用的代码:

---
output: html_document
---

# Setup{.tabset}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(DT)
```

```{r data.setup}
set.seed = 1242
rows = 64
data.1 = runif(rows, 25, 75)
data.2 = runif(rows, .01, 1)
data.3 = runif(rows, 1, 10)
classification = c("A", "B", "C", "D")
df = data.frame(cbind(data.1 = data.1, data.2 = data.2, data.3 = data.3, classification = classification))
df$data.1 = as.numeric(df$data.1)
df$data.2 = as.numeric(df$data.2)
df$data.3 = as.numeric(df$data.3)
```

```{r results= 'asis'}
for(j in levels(df$classification)){
        df.j = df[df$classification == j, ]
        cat(paste("\n\n## Classification: ", j, "##\n"))
        w = datatable(df.j)
        #datatable(df.j)
        print(w)
}
```

注意我已经注释掉了对数据表函数的直接调用,那些没有打印到 rmarkdown。写入的调用结果生成一个 html 文档,其中包含正确的选项卡,但其中没有数据表。此外,数据表实际上以正确的子集显示在我的 RStudio 会话中。作为测试,我尝试使用 knitr 的 kable 函数实现目标,表格打印在相应的选项卡中,不幸的是,kable 不具备所需的所有功能。

这不是一个完整的答案,因为其中一些仍然让我感到困惑,但至少这足以让你在我试图了解更多的时候继续前进。

---
output: html_document
---

# Setup{.tabset}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(DT)
```

```{r data.setup}
set.seed <- 1242
rows <- 64
data.1 <- runif(rows, 25, 75)
data.2 <- runif(rows, .01, 1)
data.3 <- runif(rows, 1, 10)
classification <- c("A", "B", "C", "D")
df <- data.frame(cbind(data.1 = data.1, data.2 = data.2, data.3 = data.3, classification = classification))
df$data.1 <- as.numeric(df$data.1)
df$data.2 <- as.numeric(df$data.2)
df$data.3 <- as.numeric(df$data.3)
```

```{r include = FALSE}
# Why, oh why do I need this chunk?
datatable(df)
```

```{r results = 'asis'}
for(j in unique(df$classification)){ # You were using level() here, so your for-loop never got off the ground
        df.j <- df[df$classification == j, ]
        cat(paste("\n\n## Classification: ", j, "##\n"))
        print( htmltools::tagList(datatable(df.j)) )
}

这需要第三个块才能工作,我还不确定为什么。

通过谷歌搜索同一问题到达此处。这对我有用:https://gist.github.com/ReportMort/9ccb544a337fd1778179.

基本上,生成渲染的 tibbles 列表并手动调用 knit

这是一个基于您的示例的有效 Rmd,使用了上述 link:

中的技术
---
output: html_document
---

# Setup{.tabset}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(DT)
```

```{r data.setup}
set.seed <- 1242
rows <- 64
data.1 <- runif(rows, 25, 75)
data.2 <- runif(rows, .01, 1)
data.3 <- runif(rows, 1, 10)
classification <- c("A", "B", "C", "D")
df <- data.frame(cbind(data.1 = data.1, data.2 = data.2, data.3 = data.3, classification = classification))
df$data.1 <- as.numeric(df$data.1)
df$data.2 <- as.numeric(df$data.2)
df$data.3 <- as.numeric(df$data.3)
```

```{r include = FALSE}
# prepare a list of 4 sub-dataframes, each corresponding to one classification
df_list <- split(df, df$classification)
```

```{r create-markdown-chunks-dynamically, include=FALSE}

out = NULL
for (c in names(df_list)) {
  knit_expanded <- paste0("\n\n## Classification: ", c, "##\n\n```{r results='asis', echo=FALSE}\n\ndatatable(df_list[['", c, "']])\n\n```")
  out = c(out, knit_expanded)
}

```

<!--- knit those table chunk statements --> 
`r paste(knit(text = out), collapse = '\n')`