Search code examples
rr-markdownsummarytools

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


I've already tried using the print function:

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

And also all the solutions here but they don't seem to work with html_document as the output file type of the R Markdown.


Solution

  • Olá, Inês

    The answer is in provided link. You just need to add max.tbl.height argument and specify height in pixels:

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

    Here goes an reproducible example (see # comments for tips & tricks):

    ---
    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")
    ```
    

    enter image description here

    If you have interest, check out Dominic's vignette - "Recommendations for Using summarytools With Rmarkdown".