rvisual-studio-2017summarytools

How to print.summarytools - with our without view() wrapper


I'm attempting to create a table (for inclusion into a MSFT Word table) like the one found here:

https://github.com/dcomtois/summarytools

3 - descr() : Descriptive Univariate Stats

However,

descr(iris, style = "rmarkdown")
style = "rmarkdown" is actually set as an st_options() - see below

As indicated in the text, does NOT create the table that follows in the document.

view(descr(iris, style = "rmarkdown"))

DOES create the table that follows in the document - as do the following.

view(descr(iris), "browser")
print(descr(iris), "browser")

view(descr(iris), "viewer")
print(descr(iris), "viewer")

The following create the table in a form for "Using pander with knitr" (see: http://rapporter.github.io/pander/knitr.html)

view(descr(iris), "pander")
print(descr(iris), "pander")

Near as I can tell (at this point), I need to learn knitr (https://yihui.name/knitr/) - working on it.

At the risk of being overly verbose, here is my "environment":

R version 3.5.1 (2018-07-02) -- "Feather Spray"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

library(summarytools)
library(dplyr)
library(data.table)
library(pander)
library(knitr)
library(rmarkdown)

<<<<<< sourced at start

st_options(bootstrap.css = FALSE, # Already part of the theme so no need for it
           plain.ascii = FALSE, # One of the essential settings
           style = "rmarkdown", # Idem.
           dfSummary.silent = TRUE, # Suppresses messages about temporary files
           footnote = NA, # Keeping the results minimalistic
           subtitle.emphasis = FALSE) # For the vignette theme, this gives much better results.

st_css()

library(knitr)
opts_chunk$set(comment = NA, prompt = FALSE, cache = FALSE, echo = TRUE, results = 'asis')

library(tables)


Solution

  • To have summarytools objects printed from rmarkdown with knitr, you have two options:

    1. use the markdown-style outputs by setting style = "rmarkdown" & plain.ascii = FALSE, which you did correctly using st_options(). The knitr chunk option results needs to be set to "asis":

      ```{r, results='asis'}
      descr(iris, style = "rmarkdown", plain.ascii = FALSE)
      ```
      

      Since you've set the style and plain.ascii options globally, you can omit those in the function call.


    1. You can also use the HTML rendering that makes summarytools generate the html code itself, using htmltools under the hood.

      ```{r, results='asis'}
      print(descr(iris), method = "render")
      ```
      

    It's also a very good idea to include the css in a chunk at the top of your document with the following chunk options:

    ```{r, results='asis', echo=FALSE}
    st_css()
    ```
    

    See this vignette for more info and examples.