<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/direct-integration-of-sjplot-tables-in-knitr-rmarkdown-documents-rstats/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://strengejacke.wordpress.com/2017/03/05/direct-integration-of-sjplot-tables-in-knitr-rmarkdown-documents-rstats/"> R – Strenge Jacke!</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A new update of my <strong>sjPlot</strong>-package was just released on <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=sjPlot">CRAN</a>. Thanks to <a target="_blank" rel="nofollow" href="https://twitter.com/c_schwemmer">@c_schwemmer</a>, it’s now possible to easily integrate the HTML-ouput of all table-functions into knitr-rmarkdown-documents.</p>
<h2>Simpel Tables</h2>
<p>In the past, to integrate table-output in knitr, you needed to set the argument <code>no.output = TRUE</code> and use the return-value <code>$knitr</code>:</p>
<pre>`r sjt.df(efc, no.output=TRUE)$knitr`</pre>
<p>If you also wanted to include the code to the function call, things were even more complicated. However, since sjPlot 2.3.1, you can use the table-functions like any other R-code-snippet:</p>
<pre>```{r}
sjt.frq(efc$e42dep)
```</pre>
<table>
<caption>elder’s dependency</caption>
<tr>
<th>value</th>
<th>N</th>
<th>raw %</th>
<th>valid %</th>
<th>cumulative %</th>
</tr>
<tr>
<td>independent</td>
<td>66</td>
<td>7.27</td>
<td>7.33</td>
<td>7.33</td>
</tr>
<tr>
<td>slightly dependent</td>
<td>225</td>
<td>24.78</td>
<td>24.97</td>
<td>32.30</td>
</tr>
<tr>
<td>moderately dependent</td>
<td>306</td>
<td>33.70</td>
<td>33.96</td>
<td>66.26</td>
</tr>
<tr>
<td>severely dependent</td>
<td>304</td>
<td>33.48</td>
<td>33.74</td>
<td>100.00</td>
</tr>
<tr>
<td>missings</td>
<td>7</td>
<td>0.77</td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="5">total N=908 · valid N=901 · x̄=2.94 · σ=0.94</td>
</tr>
</table>
<h2>Grouped Tables</h2>
<p>The same applies to the <code>sjtab()</code>-function, which is a „pipe“-wrapper for the sjPlot-table-functions. Here is an example how you can print multiple tables of grouped data frames in knitr. <code>sjtab()</code> calls <code>sjt.xtab()</code> (see argument <code>fun = &quot;xtab&quot;</code>) and uses the first two arguments of the data frame as variables for the contingency table. Since the data frame is grouped (by gender and education), a table for each group is displayed. The group-characteristic is printed as table-caption. Note that the HTML-output here is truncated and limited to the first three tables only.</p>
<pre>```{r}
library(dplyr)
library(sjPlot)
library(sjmisc)
data(efc)

efc %>% 
  group_by(e16sex, c172code) %>% 
  select(e42dep, n4pstu, e16sex, c172code) %>% 
  sjtab(fun = &quot;xtab&quot;)
```</pre>
<table>
<caption>elder’s gender: male<br>carer’s level of education: low level of education</caption>
<tr>
<th rowspan="2">elder’s dependency</th>
<th colspan="5">Care level</th>
<th rowspan="2">Total</th>
</tr>
<tr>
<td>No Care Level</td>
<td>Care Level 1</td>
<td>Care Level 2</td>
<td>Care Level 3</td>
<td>Care Level 3+</td>
</tr>
<tr>
<td>independent</td>
<td><span>4</span></td>
<td><span>1</span></td>
<td><span>0</span></td>
<td><span>0</span></td>
<td><span>0</span></td>
<td><span>5</span></td>
</tr>
<tr>
<td>slightly dependent</td>
<td><span>9</span></td>
<td><span>3</span></td>
<td><span>1</span></td>
<td><span>2</span></td>
<td><span>0</span></td>
<td><span>15</span></td>
</tr>
<tr>
<td>moderately dependent</td>
<td><span>15</span></td>
<td><span>6</span></td>
<td><span>4</span></td>
<td><span>2</span></td>
<td><span>0</span></td>
<td><span>27</span></td>
</tr>
<tr>
<td>severely dependent</td>
<td><span>8</span></td>
<td><span>5</span></td>
<td><span>10</span></td>
<td><span>7</span></td>
<td><span>0</span></td>
<td><span>30</span></td>
</tr>
<tr>
<td>Total</td>
<td><span>36</span></td>
<td><span>15</span></td>
<td><span>15</span></td>
<td><span>11</span></td>
<td><span>0</span></td>
<td><span>77</span></td>
</tr>
<td colspan="7">χ<sup>2</sup>=13.843 · df=9 · Cramer’s V=0.245 · Fisher’s p=0.141</td>

</table>
<p> </p>
<table>
<caption>elder’s gender: male<br>carer’s level of education: intermediate level of education</caption>
<tr>
<th rowspan="2">elder’s dependency</th>
<th colspan="5">Care level</th>
<th rowspan="2">Total</th>
</tr>
<tr>
<td>No Care Level</td>
<td>Care Level 1</td>
<td>Care Level 2</td>
<td>Care Level 3</td>
<td>Care Level 3+</td>
</tr>
<tr>
<td>independent</td>
<td><span>7</span></td>
<td><span>2</span></td>
<td><span>4</span></td>
<td><span>2</span></td>
<td><span>0</span></td>
<td><span>15</span></td>
</tr>
<tr>
<td>slightly dependent</td>
<td><span>22</span></td>
<td><span>7</span></td>
<td><span>8</span></td>
<td><span>2</span></td>
<td><span>0</span></td>
<td><span>39</span></td>
</tr>
<tr>
<td>moderately dependent</td>
<td><span>27</span></td>
<td><span>14</span></td>
<td><span>13</span></td>
<td><span>4</span></td>
<td><span>1</span></td>
<td><span>59</span></td>
</tr>
<tr>
<td>severely dependent</td>
<td><span>7</span></td>
<td><span>3</span></td>
<td><span>12</span></td>
<td><span>20</span></td>
<td><span>1</span></td>
<td><span>43</span></td>
</tr>
<tr>
<td>Total</td>
<td><span>63</span></td>
<td><span>26</span></td>
<td><span>37</span></td>
<td><span>28</span></td>
<td><span>2</span></td>
<td><span>156</span></td>
</tr>
<td colspan="7">χ<sup>2</sup>=42.707 · df=12 · Cramer’s V=0.302 · Fisher’s p=0.000</td>

</table>
<p> </p>
<table>
<caption>elder’s gender: male<br>carer’s level of education: high level of education</caption>
<tr>
<th rowspan="2">elder’s dependency</th>
<th colspan="5">Care level</th>
<th rowspan="2">Total</th>
</tr>
<tr>
<td>No Care Level</td>
<td>Care Level 1</td>
<td>Care Level 2</td>
<td>Care Level 3</td>
<td>Care Level 3+</td>
</tr>
<tr>
<td>independent</td>
<td><span>1</span></td>
<td><span>0</span></td>
<td><span>0</span></td>
<td><span>0</span></td>
<td><span>0</span></td>
<td><span>1</span></td>
</tr>
<tr>
<td>slightly dependent</td>
<td><span>8</span></td>
<td><span>1</span></td>
<td><span>2</span></td>
<td><span>2</span></td>
<td><span>0</span></td>
<td><span>13</span></td>
</tr>
<tr>
<td>moderately dependent</td>
<td><span>11</span></td>
<td><span>2</span></td>
<td><span>5</span></td>
<td><span>0</span></td>
<td><span>0</span></td>
<td><span>18</span></td>
</tr>
<tr>
<td>severely dependent</td>
<td><span>4</span></td>
<td><span>2</span></td>
<td><span>3</span></td>
<td><span>2</span></td>
<td><span>0</span></td>
<td><span>11</span></td>
</tr>
<tr>
<td>Total</td>
<td><span>24</span></td>
<td><span>5</span></td>
<td><span>10</span></td>
<td><span>4</span></td>
<td><span>0</span></td>
<td><span>43</span></td>
</tr>
<td colspan="7">χ<sup>2</sup>=5.992 · df=9 · Cramer’s V=0.216 · Fisher’s p=0.620</td>

</table>
<p><em>(… remaining table output truncated)</em></p>
<p>The knitr-integration works for all table-functions in sjPlot. You can find some examples <a target="_blank" rel="nofollow" href="http://strengejacke.de/sjPlot/">over here</a>.</p>
<p> Tagged: <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/tag/knitr/">knitr</a>, <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/tag/markdown/">markdown</a>, <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/tag/r/">R</a>, <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/tag/rmarkdown/">rmarkdown</a>, <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/tag/rstats/">rstats</a>, <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/tag/sjplot/">sjPlot</a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/strengejacke.wordpress.com/1245/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/strengejacke.wordpress.com/1245/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://strengejacke.wordpress.com/2017/03/05/direct-integration-of-sjplot-tables-in-knitr-rmarkdown-documents-rstats/"> R – Strenge Jacke!</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/hRiBAkUabv8"></a></div>