5

我报告了一个问题https://github.com/rstudio/rmarkdown/issues/967并且想知道是否有解决方法(如何使其工作)?

在此处输入图像描述

下面的可重现示例(改变 n 和 nGroup 以查看效果 - 当 n = 100 和 nGroup=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)
```
4

1 回答 1

7

查看您的示例生成的 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
---
于 2017-02-23T09:00:33.563 回答