为什么在rmarkdown中循环时无法显示数据表? [英] Why does datatable not print when looping in rmarkdown?

查看:434
本文介绍了为什么在rmarkdown中循环时无法显示数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个动态的rmarkdown文档.最终结果应为数据中的每个分类"创建一个选项卡.每个选项卡都应具有一个数据表,该数据表来自DT包,并已打印数据.下面是我一直在使用的代码:

I am working on creating a dynamic rmarkdown document. The end result should create a tab for each 'classification' in the data. Each tab should have a datatable, from the DT package, with the data printed to it. Below is the code I have been using:

---
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)
}
```

注意,我已注释掉对datatable函数的直接调用,这些调用未打印到rmarkdown.编写的调用结果将生成带有正确选项卡的html文档,但其中没有数据表.此外,数据表实际上以正确的子设置显示在我的RStudio会话中.作为测试,我尝试使用knitr的kable函数实现目标,并将表格打印在其相应的选项卡中,不幸的是,kable并没有所需的全部功能.

Notice I have commented out straight calls to the datatable function, those were not printing to rmarkdown. The results of the call as written generate an html document with the correct tabs, but no datatables in them. Additionally, the datatables actually display in my RStudio session with the correct subsetting. As a test, I tried achieving the goal using the kable function from knitr, and the tables were printed in their appropriate tabs, unfortunately, kable does not have all the functionality required.

推荐答案

这并不是一个完整的答案,因为其中有些问题仍然让我感到困惑,但是至少这足以让您在我尝试了解更多内容的同时继续前进.

This is not a complete answer as some of this is still puzzling me, but at least this is good enough to get you going while I try to understand some more.

---
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)) )
}

此功能需要第三个块,我不确定为什么.

The third chunk is required for this to work, I'm not yet sure why.

这篇关于为什么在rmarkdown中循环时无法显示数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆