This vignette describes some basic features of all sjt
functions from the sjPlot package. These functions create HTML table outputs.
Before starting, we need to load the required packages and sample data.
# load packages
library(sjPlot)
library(sjmisc)
library(dplyr)
# load sample data set.
data(efc)
All sjt
functions create a HTML page with the data table. This table, by default, is opened in the viewer pane of your IDE (in case you’re using an IDE which also supports the viewer pane, see parameter use.viewer
for details). If a viewer pane is not available, the created HTML output is saved as temporary file and opened in your default webbrowser. The temporary files are deleted after your R session ends.
You can save the HTML page as file for further usage by specifying the file
parameter. The saved HTML file can be opened by word processors like LibreOffice or Microsoft Office.
You can directly drag and drop a table from the RStudio viewer pane oder browser into your word processor. Simply select the complete table with your mouse and drag it into office.
In some cases, you may have to specify a character encoding in order to get proper labels in the HTML tables. If you face problems with correctly displaying labels, use the encoding
parameter to change the character encoding. This value dependes on your region where you live. Following example, which works for western European countries, is the default behaviour of all sjt
-functions:
# don't need to do this, because all sjt-functions
# use this code as default encoding-detection
if (.Platform$OS.type == "unix")
encoding <- "UTF-8"
else
encoding <- "Windows-1252"
sjt.frq(efc$e15relat, encoding = encoding)
This example first detects your operating system and then chooses the associated character encoding, which is used in the HTML file. If this does not work for you, you have to use the encoding
parameter.
sjPlot provides a print-method for HTML-tables to use in knitr-documents, so you can use the sjt
-functions inside code chunks in knitr-rmarkdown files. The HTML-table will be printed to the knitr-document ‘as is’:
sjt.frq(efc$e42dep)
#> `sjt.frq()` will become deprecated in the future. Please use `sjmisc::frq(out = "v")` instead.
value | N | raw % | valid % | cumulative % |
---|---|---|---|---|
independent | 66 | 7.27 | 7.33 | 7.33 |
slightly dependent | 225 | 24.78 | 24.97 | 32.30 |
moderately dependent | 306 | 33.70 | 33.96 | 66.26 |
severely dependent | 304 | 33.48 | 33.74 | 100.00 |
missings | 7 | 0.77 | ||
total N=908 · valid N=901 · x̄=2.94 · σ=0.94 |
This also works for table-outputs from sjtab()
. For instance, you can easily create multiple tables from grouped data frames.
efc %>%
group_by(e16sex, c172code) %>%
select(e16sex, c172code, e42dep) %>%
sjtab()
#> `sjt.frq()` will become deprecated in the future. Please use `sjmisc::frq(out = "v")` instead.
#> `sjt.frq()` will become deprecated in the future. Please use `sjmisc::frq(out = "v")` instead.
#> `sjt.frq()` will become deprecated in the future. Please use `sjmisc::frq(out = "v")` instead.
value | N | raw % | valid % | cumulative % |
---|---|---|---|---|
independent | 5 | 6.25 | 6.25 | 6.25 |
slightly dependent | 16 | 20.00 | 20.00 | 26.25 |
moderately dependent | 28 | 35.00 | 35.00 | 61.25 |
severely dependent | 31 | 38.75 | 38.75 | 100.00 |
missings | 0 | 0.00 | ||
total N=80 · valid N=80 · x̄=3.06 · σ=0.92 |
value | N | raw % | valid % | cumulative % |
---|---|---|---|---|
independent | 15 | 9.62 | 9.62 | 9.62 |
slightly dependent | 39 | 25.00 | 25.00 | 34.62 |
moderately dependent | 59 | 37.82 | 37.82 | 72.44 |
severely dependent | 43 | 27.56 | 27.56 | 100.00 |
missings | 0 | 0.00 | ||
total N=156 · valid N=156 · x̄=2.83 · σ=0.94 |
value | N | raw % | valid % | cumulative % |
---|---|---|---|---|
independent | 1 | 2.33 | 2.33 | 2.33 |
slightly dependent | 13 | 30.23 | 30.23 | 32.56 |
moderately dependent | 18 | 41.86 | 41.86 | 74.42 |
severely dependent | 11 | 25.58 | 25.58 | 100.00 |
missings | 0 | 0.00 | ||
total N=43 · valid N=43 · x̄=2.91 · σ=0.81 |
value | N | raw % | valid % | cumulative % |
---|---|---|---|---|
independent | 7 | 7.07 | 7.07 | 7.07 |
slightly dependent | 25 | 25.25 | 25.25 | 32.32 |
moderately dependent | 33 | 33.33 | 33.33 | 65.66 |
severely dependent | 34 | 34.34 | 34.34 | 100.00 |
missings | 0 | 0.00 | ||
total N=99 · valid N=99 · x̄=2.95 · σ=0.94 |
value | N | raw % | valid % | cumulative % |
---|---|---|---|---|
independent | 30 | 8.57 | 8.57 | 8.57 |
slightly dependent | 96 | 27.43 | 27.43 | 36.00 |
moderately dependent | 104 | 29.71 | 29.71 | 65.71 |
severely dependent | 120 | 34.29 | 34.29 | 100.00 |
missings | 0 | 0.00 | ||
total N=350 · valid N=350 · x̄=2.90 · σ=0.98 |
value | N | raw % | valid % | cumulative % |
---|---|---|---|---|
independent | 4 | 3.54 | 3.54 | 3.54 |
slightly dependent | 26 | 23.01 | 23.01 | 26.55 |
moderately dependent | 44 | 38.94 | 38.94 | 65.49 |
severely dependent | 39 | 34.51 | 34.51 | 100.00 |
missings | 0 | 0.00 | ||
total N=113 · valid N=113 · x̄=3.04 · σ=0.85 |
Note that this does not properly work for PDF-documents, but only if the output of the rmarkdown-file is HTML.
The table output is in in HTML format. The table style (visual appearance) is formatted using Cascading Style Sheets. If you are a bit familiar with these topics, you can easily customize the appearance of the table output.
Many table elements (header, row, column, cell, summary row, first row or column…) have CSS-class attributes, which can be used to change the table style. Since each sjt
function has different table elements and thus different class attributes, you first need to know which styles can be customized.
Each sjt
function invisibly returns several values. The return value page.style
contains the style information for the HTML table. You can print this style sheet to console using the base R cat()
function:
cat(sjt.frq(efc$e42dep, no.output = TRUE)$page.style)
#> <style>
#> html, body { background-color: white; }
#> table { border-collapse:collapse; border:none; }
#> .thead { border-top:double; text-align:center; font-style:italic; font-weight:normal; padding-left:0.2cm; padding-right:0.2cm; }
#> .tdata { padding:0.2cm; }
#> .summary { text-align:right; font-style:italic; font-size:0.9em; padding-top:0.1cm; padding-bottom:0.1cm; }
#> .arc { background-color:#eaeaea; }
#> .qrow { border-bottom: 1px solid #cc3333; }
#> .mdrow { font-weight:bolder; font-style:italic; color:#993333; }
#> .abstand { margin-bottom: 2em; }
#> .lasttablerow { border-top:1px solid; border-bottom:double; }
#> .firsttablerow { border-bottom:1px solid; }
#> .leftalign { text-align:left; }
#> .centeralign { text-align:center; }
#> caption { font-weight: bold; text-align:left; }
#> .firsttablecol { }
#> </style>
The HTML code is obtained by using the page.content
return value. Since the sjt.frq()
function allows to plot multiple tables at once, this function returns a list of HTML tables as page.content.list
. The following code prints the HTML code of the table to the R console:
cat(sjt.frq(efc$e42dep, no.output = TRUE)$page.content.list[[1]])
#> `sjt.frq()` will become deprecated in the future. Please use `sjmisc::frq(out = "v")` instead.
# not that other sjt-functions don't return a page.content-list, but
# just '$page.content'...
Now you can see which table elements are associated with which CSS class attributes. If you compare the page.style
with the related page.content
, you see that not all style attributes are used:
.arc
is the attribute for the parameter altr.row.col
and appears only in the page.content
if it is set to TRUE
..qrow
and .mdrow
are the class attributes for the emph.quart
and emph.md
parameters..firsttablecol
is empty, but used. This class attribute is assigned in case you want to customize the appearance of the first table column (the one with value labels).You can customize the table output with the CSS
parameter. This parameter requires a list of attributes, which follow a certain pattern:
css.
prefixcaption
, thead
, centeralign
, arc
etc.)Example:
sjt.frq(
efc$e42dep,
CSS = list(css.centeralign = 'text-align: left;',
css.caption = 'font-weight: normal; font-style: italic;',
css.firsttablecol = 'font-weight: bold;',
css.lasttablerow = 'border-top: 1px solid; border-bottom: none;',
css.summary = 'color: blue;')
)
#> `sjt.frq()` will become deprecated in the future. Please use `sjmisc::frq(out = "v")` instead.
value | N | raw % | valid % | cumulative % |
---|---|---|---|---|
independent | 66 | 7.27 | 7.33 | 7.33 |
slightly dependent | 225 | 24.78 | 24.97 | 32.30 |
moderately dependent | 306 | 33.70 | 33.96 | 66.26 |
severely dependent | 304 | 33.48 | 33.74 | 100.00 |
missings | 7 | 0.77 | ||
total N=908 · valid N=901 · x̄=2.94 · σ=0.94 |
In the above example, the summary-table row lost the original style and just became blue. If you want to keep the original style and just add additional style information, use the plus-sign (+
) as initial character for the parameter attributes. In the following example, the summary row keeps its original style and is additionally printed in blue:
sjt.frq(efc$e42dep, CSS = list(css.summary = '+color: blue;'))
value | N | raw % | valid % | cumulative % |
---|---|---|---|---|
independent | 66 | 7.27 | 7.33 | 7.33 |
slightly dependent | 225 | 24.78 | 24.97 | 32.30 |
moderately dependent | 306 | 33.70 | 33.96 | 66.26 |
severely dependent | 304 | 33.48 | 33.74 | 100.00 |
missings | 7 | 0.77 | ||
total N=908 · valid N=901 · x̄=2.94 · σ=0.94 |