r语言 - 为什么在循环标记时不打印datatable



我正在创建一个动态标记文档。最终结果应该为数据中的每个"分类"创建一个选项卡。每个选项卡都应该有一个来自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("nn## Classification: ", j, "##n"))
        w = datatable(df.j)
        #datatable(df.j)
        print(w)
}
```

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

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

---
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("nn## Classification: ", j, "##n"))
        print( htmltools::tagList(datatable(df.j)) )
}

第三块是这个工作所必需的,我还不确定为什么。

通过谷歌搜索同样的问题到达这里。这对我很有效:https://gist.github.com/ReportMort/9ccb544a337fd1778179.

基本上,生成一个渲染图列表并手动调用knit

下面是基于您的示例的工作Rmd,使用在上面的链接中找到的技术:

---
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("nn## Classification: ", c, "##nn```{r results='asis', echo=FALSE}nndatatable(df_list[['", c, "']])nn```")
  out = c(out, knit_expanded)
}
```
<!--- knit those table chunk statements --> 
`r paste(knit(text = out), collapse = 'n')`

相关内容

  • 没有找到相关文章