Direct integration of sjPlot-tables in knitr-rmarkdown-documents #rstats

A new update of my sjPlot-package was just released on CRAN. Thanks to @c_schwemmer, it’s now possible to easily integrate the HTML-ouput of all table-functions into knitr-rmarkdown-documents.

Simpel Tables

In the past, to integrate table-output in knitr, you needed to set the argument no.output = TRUE and use the return-value $knitr:

`r sjt.df(efc, no.output=TRUE)$knitr`

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:

```{r}
sjt.frq(efc$e42dep)
```
elder’s dependency
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

Grouped Tables

The same applies to the sjtab()-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. sjtab() calls sjt.xtab() (see argument fun = "xtab") 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.

```{r}
library(dplyr)
library(sjPlot)
library(sjmisc)
data(efc)

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

 

elder’s gender: male
carer’s level of education: intermediate level of education
elder’s dependency Care level Total
No Care Level Care Level 1 Care Level 2 Care Level 3 Care Level 3+
independent 7 2 4 2 0 15
slightly dependent 22 7 8 2 0 39
moderately dependent 27 14 13 4 1 59
severely dependent 7 3 12 20 1 43
Total 63 26 37 28 2 156
χ2=42.707 · df=12 · Cramer’s V=0.302 · Fisher’s p=0.000

 

elder’s gender: male
carer’s level of education: high level of education
elder’s dependency Care level Total
No Care Level Care Level 1 Care Level 2 Care Level 3 Care Level 3+
independent 1 0 0 0 0 1
slightly dependent 8 1 2 2 0 13
moderately dependent 11 2 5 0 0 18
severely dependent 4 2 3 2 0 11
Total 24 5 10 4 0 43
χ2=5.992 · df=9 · Cramer’s V=0.216 · Fisher’s p=0.620

(… remaining table output truncated)

The knitr-integration works for all table-functions in sjPlot. You can find some examples over here.

Advertisements

7 Kommentare zu „Direct integration of sjPlot-tables in knitr-rmarkdown-documents #rstats

  1. What works is opening the HTML-file in a word-processor and saving this one to PDF – not the most elegant workaround. But the table-functions in sjPlot only return HTML-code, because the functions are primarily intended to work with the RStudio-Viewer or webbrowser, and for copy/pasting into word-processors (MS Word, LibreOffice…). I’m not that familar with LaTex and thought that there were enough packages that create tables for PDF-output. 🙂

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s