【问题标题】:Is there a way to add a scroll bar to dfSummary output in R markdown file?有没有办法在 R markdown 文件中的 dfSummary 输出中添加滚动条?
【发布时间】:2020-05-24 11:41:27
【问题描述】:

我已经尝试过使用print 函数:

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

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

【问题讨论】:

    标签: r r-markdown summarytools


    【解决方案1】:

    奥拉,伊内斯

    答案在提供的链接中。您只需要添加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 - “使用 Rmarkdown 使用 summarytools 的建议”。

    【讨论】:

    • 嘿!感谢您的回复。我选择使用DT::datatable() 选择多个页面一次只显示5 个条目。 print() 的问题在于它使表格窗口大于页面宽度。不过还是非常感谢!
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 2011-11-22
      • 2022-07-22
      • 1970-01-01
      相关资源
      最近更新 更多