Pete
Pete

Reputation: 321

How to I get tab_model to display properly in Rmarkdown PDF?

I realize this is probably quite simple, but I have looked and can't find an example of how to use sjPlot::tab_model in an Rmarkdown document sent to PDF.

Here is reproducible code:

---
title: "test"
author: "Me"
date: "2020/12/6"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(sjPlot)

A table

tab_model(lm(mpg~cyl, data=mtcars))

And what I get looks something like this:

A table tab_model(lm(mpg~cyl, data=mtcars)) mpg Predictors Estimates CI p (Intercept) 37.88 33.65 – 42.12 <0.001 cyl -2.88 -3.53 – -2.22 <0.001 Observations 32 R2 / R2 adjusted

Upvotes: 3

Views: 5377

Answers (2)

Gorka
Gorka

Reputation: 4043

This is a well known issue, and the creator of the package (Daniel) responded here: https://github.com/strengejacke/sjPlot/issues/712

In the meantime, I ended up creating a couple functions to help out.

Please see the Github repo https://github.com/gorkang/html2latex/.

In the repo you can find an example.Rmd file with step-by-step instructions. It works in a Ubuntu 20.04 system.

Upvotes: 1

Dylan_Gomes
Dylan_Gomes

Reputation: 2242

I have tried to save the html within the tab_model function and then read it into the Rmd doc Include HTML files in R Markdown file?.

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(sjPlot)
```

```{r plot}

tab_model(lm(mpg~cyl, data=mtcars),file="test.html")

```


```{r, echo=FALSE}
htmltools::includeHTML("test.html")
```

But I get the same problem, which makes me think there is some option for formatting html tables in Rmd, but I can't seem to find a solution.

There is a related post here: https://stackoverflow.com/a/33182503/9096420, but tab_model doesn't work well with the code there, because it requires a data frame.

I think there is a bit of a hack that works, but isn't ideal. You can save the output as a png and then include it in the Rmd doc https://stackoverflow.com/a/25167279/9096420.

---
title: "Untitled"
author: "Dylan Gomes"
date: "12/7/2020"
output: pdf_document
---
![Caption for the picture.](test.png) ## this file 'test.png' needs to be in the working directory.

enter image description here

Upvotes: 1

Related Questions