2017-02-21 48 views
5

我報告了一個問題https://github.com/rstudio/rmarkdown/issues/967,我想知道是否有解決方法(如何使這項工作)?Rmarkdown重疊輸出

enter image description here

下面

重複的例子,(變化n和n組看到效果 - 沒有重疊,當n = 100和n組= 10):

--- 
title: "Test links to sections in DT" 
output: html_document 
--- 

```{r setup, include=FALSE} 
knitr::opts_chunk$set(echo=FALSE) 
knitr::opts_chunk$set(message=FALSE) 
knitr::opts_chunk$set(warning=FALSE) 

## DT Test 

```{r echo=FALSE} 
library(DT) 

n <- 1000 
nGroup <- 100 

testDF <- data.frame(text=paste0("Section", 1:n), 
        number=1:n, 
        group=rep(1:(n/nGroup), n/nGroup)) 

datatable(head(testDF), caption="Whole table", rownames=FALSE, escape=FALSE, options=list(paging=FALSE, info=FALSE)) 

getDT<-function(x) { 
    a <- list() 
    a[[1]] <- htmltools::tags$h3("test1") 
    a[[2]] <- datatable(x[, c("text", "number")], caption=htmltools::tags$caption(style="caption-side: top; text-align: left;", "Group: ", htmltools::strong(x$group)), rownames=FALSE, escape=FALSE, filter=c("none"), options=list(paging=FALSE, info=FALSE)) 
    a[[3]] <- htmltools::tags$h4("test1") 

    return(a) 
} 

res <- lapply(split(testDF, testDF$group), getDT) 

htmltools::tagList(res) 
``` 

回答

5

看你的示例產生HTML,我看到一堆div標籤看起來像這樣:

<div class="datatables html-widget html-widget-static-bound" 
    id="htmlwidget-3efe8ca4aa087193f03e" 
    style="width:960px;height:500px;"> 

注意內嵌樣式設置高度500個像素。然而,div內的內容遠高於500像素,因此它已經溢出了div的邊界。

我不確定500px是從哪裏來的,但作爲解決方法,您可以用不同的樣式來覆蓋它。例如,在你的RMarkdown的頂部(頭後)補充一點:

<style type="text/css"> 
    div.datatables { height: auto !important;} 
</style> 

或者,如果你希望將自己的RMarkdown整潔的CSS,把

div.datatables { 
    height: auto !important; 
} 

在一個單獨的CSS文件,並在RMarkdown標題中鏈接到它,如下所示:

--- 
title: "Test links to sections in DT" 
output: 
    html_document: 
    css: overlap_workaround.css 
---