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 package
library(sjPlot)
library(sjmisc)
# 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 useViewer
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.
Most sjt
function invisibly return a structure with following values:
page.style
page.content
output.complete
knitr
page.style
and page.content
are described below in the CSS section. If you want to include a HTML table in your knitr document, simply use an inline code chunk. Be sure to use no.output=TRUE
, because else your output would also be opened in a viewer pane or webbrowser.
sjt.frq(efc$e42dep, no.output=TRUE)$knitr
If you want to include both the source code and the output in your HTML document, you first need to write your code chunk, which should not be evaluated. Note that you don’t need to use no.output = TRUE
and $knitr
in this chunk.
Chunk 1
```{r eval=FALSE}
sjt.frq(efc$e42dep)
```
Then repeat your code line, using the no.output = TRUE
and $knitr
parameters. This code chunk, however, should be evaluated:
Chunk 2
sjt.frq(efc$e42dep, no.output=TRUE)$knitr
If the above two code chunks 1 and 2 are compiled, the results look like this in your HTML document:
sjt.frq(efc$e42dep)
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 |
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.
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 |
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>
#> 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]])
#> <table>
#> <caption>elder's dependency</caption>
#> <tr>
#> <th class="thead firsttablerow firsttablecol">value</th>
#> <th class="thead firsttablerow">N</th>
#> <th class="thead firsttablerow">raw %</th>
#> <th class="thead firsttablerow">valid %</th>
#> <th class="thead firsttablerow">cumulative %</th>
#> </tr>
#>
#> <tr>
#> <td class="tdata leftalign firsttablecol">independent</td>
#> <td class="tdata centeralign">66</td>
#> <td class="tdata centeralign">7.27</td>
#> <td class="tdata centeralign">7.33</td>
#> <td class="tdata centeralign">7.33</td>
#> </tr>
#>
#> <tr>
#> <td class="tdata leftalign firsttablecol">slightly dependent</td>
#> <td class="tdata centeralign">225</td>
#> <td class="tdata centeralign">24.78</td>
#> <td class="tdata centeralign">24.97</td>
#> <td class="tdata centeralign">32.30</td>
#> </tr>
#>
#> <tr>
#> <td class="tdata leftalign firsttablecol">moderately dependent</td>
#> <td class="tdata centeralign">306</td>
#> <td class="tdata centeralign">33.70</td>
#> <td class="tdata centeralign">33.96</td>
#> <td class="tdata centeralign">66.26</td>
#> </tr>
#>
#> <tr>
#> <td class="tdata leftalign firsttablecol">severely dependent</td>
#> <td class="tdata centeralign">304</td>
#> <td class="tdata centeralign">33.48</td>
#> <td class="tdata centeralign">33.74</td>
#> <td class="tdata centeralign">100.00</td>
#> </tr>
#>
#> <tr>
#> <td class="tdata leftalign lasttablerow firsttablecol">missings</td>
#> <td class="tdata centeralign lasttablerow">7</td>
#> <td class="tdata centeralign lasttablerow">0.77</td>
#> <td class="tdata lasttablerow"></td>
#> <td class="tdata lasttablerow"></td>
#> </tr>
#> <tr>
#> <td class="tdata summary" colspan="5">total N=908 · valid N=901 · x̄=2.94 · σ=0.94</td>
#> </tr>
#> </table>
# not that other sjt-functions don't return a 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;'))
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 |