有没有办法在 R markdown 文件中的 dfSummary 输出中添加滚动条?

Is there a way to add a scroll bar to dfSummary output in R markdown file?

我已经尝试使用 print 函数:

print(dfSummary(df), method = "render")

还有所有解决方案here,但它们似乎不适用于 html_document 作为 R Markdown 的输出文件类型。

我可以建议使用包 "DT",如果您绝对需要 dfSummary 我可以再试一次,但需要最少的代码来复制您的示例。 DT 具有搜索功能,同时允许用户控制他们在查看数据时看到的行数。

  ---
  title: "DF summary"
  author: "Whosebug"
  date: "5/24/2020"
  output: html_document
  ---
  ```{r}
  library(DT)
  datatable(head(mtcars, 30), options = list(
  order = list(list(2, 'asc'), list(4, 'desc'))
  ))
  ```

Olá, Inês

答案已提供link。您只需要添加 max.tbl.height 参数并以像素为单位指定高度:

print(dfSummary(df),
      max.tbl.height = 250,
      method = "render")

这是一个可重现的示例(有关提示和技巧,请参阅 # comments):

---
title: "Title"
author: "Author"
date: "26/05/2020"
output: 
  html_document:
    toc: TRUE
    toc_float: TRUE
---

```{r setup, include = FALSE}
library(knitr)
library(summarytools)
knitr::opts_chunk$set(results = "asis")
```

### Adding a scrollbar to dfSummary

```{r summarytools-css, echo = FALSE}
# with summarytools’ CSS we can cancel Bootstrap’s CSS (both are included by default)
# without it odd layout are expected, especially with dfSummary()
st_css(bootstrap = FALSE)
```

```{r dfSummary, echo = FALSE}
print(dfSummary(tobacco, 
                style = "grid",           # set style to “grid”
                valid.col = FALSE,        # drop Valid column if redundant
                graph.magnif = 0.82),     # zoom factor (max = 1) for bar plots and histograms
      headings = FALSE,
      footnote = NA,
      # use maximum table (specified) height (in pixels) and wrap the results in a scrollable window
      max.tbl.height = 300,            
      method = "render")
```

如果您有兴趣,请查看 Dominic 的 vignette - "Recommendations for Using summarytools With Rmarkdown"。