2017-02-21 145 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 
---