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

[This article was first published on R – Strenge Jacke!, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.


Tagged: knitr, markdown, R, rmarkdown, rstats, sjPlot

To leave a comment for the author, please follow the link and comment on their blog: R – Strenge Jacke!.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)