Using simpletable and broom together

The broom package is a great R package for neatly summarizing the results of statistical tests and models. The output of broom is always a data.frame and rownames are always included as the first column. This makes them work very seamlessly with simpletable if we want to produce an attractive looking table in a RMarkdown document.


For background information on simpletable see the introductory vignette here or the github page here.



Load packages

library(broom)
library(simpletable)




Some examples

I will first use the tidy function in broom to make some summary tables of a lm and glm model.



lmfit <- lm(mpg ~ wt, mtcars)
lmfit


tidy(lmfit)



This is how we can make it look using simpletable using two default styles:



tablecode(tidy(lmfit), tabletype="minimal")



tablecode(tidy(lmfit), tabletype="box")



Here is another example:

glmfit <- glm(am ~ wt, mtcars, family="binomial")
glmfit
tidy(glmfit)


tablecode(tidy(glmfit), tabletype="minimal")


tablecode(tidy(glmfit), tabletype="box")



We can also look at the fitted values and residuals with augment


lmfit.a <-augment(lmfit)
lmfit.a[,4:10] <- round(lmfit.a[,4:10],3)


which can give..



tablecode(lmfit.a, tabletype="gridtable")




Customizing the output

It's still a work in process, but I'm also enabling the user to customize output of tables to change all sorts of features. This can be done with the stylecode function. A vignette is in the works. Here is a brief example:

stylecode(name="uniqueid", colh="rgba(0,0,255,0.2)", colh.text="#000000", col="rgba(0,0,255,0.1)", col.text="#000000", font.size=c(14,12), hfont.wt=900, lineheight=1.5, border.style="none", borderh.style="solid", borderh.wt=2, borderh.col="#3399FF", htext.align="center", text.align="center", borderb.style="solid", pad=c(3,1,3,3))

tablecode(lmfit.a, tabletype="uniqueid", width="35%")



I still have some features to add to this function such as the ability to manipulate the width of the first column independently of the other columns, but this gives you an idea of what's possible.






jalapic/simpletable documentation built on May 18, 2019, 11:18 a.m.