2016-09-27 4 views
3

동적 rmarkdown 문서를 만드는 중입니다. 최종 결과는 데이터의 각 '분류'에 대한 탭을 작성해야합니다. 각 탭에는 데이터가 인쇄 된 DT 패키지의 데이터 테이블이 있어야합니다. 아래는 내가 사용 된 코드는 다음과 같습니다rmarkdown에서 루핑 할 때 datatable이 인쇄되지 않는 이유는 무엇입니까?

--- 
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에 인쇄되지 않은, DataTable의 기능을 바로 호출을 주석했다. 작성된 호출 결과는 올바른 탭이 있지만 html 문서가없는 html 문서를 생성합니다. 또한 데이터 테이블은 실제로 올바른 서브 세트로 RStudio 세션에 표시됩니다. 테스트로서 knitr의 kable 함수를 사용하여 목표를 달성하려고 시도했지만 테이블이 적절한 탭에 인쇄되었지만 유감스럽게도 kable에 필요한 모든 기능이 없습니다.

답변

3

이 중 일부는 여전히 나를 혼란 스럽기 때문에 완전한 대답은 아니지만, 적어도이 점은 나에게 더 많은 것을 이해하려고 노력하는 동안 당신을 끌어들일만큼 충분합니다.

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

세 번째 청크가 작동하려면 필요합니다. 아직 확실하지 않습니다.

+0

한 내가 간단한 솔루션을 널리 검색 봤는데이 그 것이었다 : 여기

은 위의 링크에있는 기술을 사용하여, 당신의 예제를 기반으로 작업 RMD이다. 세 번째 청크에서'datatable (df [1,]) '을 입력하면 여전히 작동합니다! @yihui가 왜 이것이 필요한지 알고 있는지 궁금합니다. – bouncyball

1

여기에 동일한 질문을 통해 연락하십시오. 이것은 나를 위해 일했다 : https://gist.github.com/ReportMort/9ccb544a337fd1778179.

기본적으로 렌더링 된 티블 목록을 생성하고 knit을 수동으로 호출하십시오.

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