-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.Rmd
142 lines (92 loc) · 3.27 KB
/
example.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
---
title: "Using sjPlot::tab_model() in a Rmd document"
author: "Gorka Navarrete @gorkang"
output:
pdf_document:
toc: yes
header-includes:
- \usepackage{array}
- \usepackage{longtable}
- \newcommand\textstyleStrongEmphasis[1]{\textbf{#1}}
- \makeatletter
- \newcommand\arraybslash{\let\\\@arraycr}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\vspace{1cm}
sjPlot::tab_model() is wonderful to create beautiful tables for your statistical analysis but, afaik, it is not possible to easily save the output html tables as tex or pdf documents, or include them in Rmd documents.
In the [html2latex](https://github.com/gorkang/html2latex/) repo there are two functions to help you simplify the process.
## tab_model() html tables to tex and pdf
Using `html2pdf()` with an html table will create a tex file. The `build_pdf` parameter builds a pdf using that tex file.
```{r message=FALSE, warning=FALSE}
library(html2latex)
library(lme4)
library(sjPlot)
# This is a terrible model
model = lmer(mpg ~ cyl * disp + (1|vs), mtcars)
# We save the sjPlot table to an .html file
sjPlot::tab_model(model, file = "temp.html")
# Create tex and pdf
# This readme was knitted on a Mac, therefore mac = TRUE
html2pdf(filename = "temp.html", table_width = 13, silent = TRUE, style = TRUE, build_pdf = TRUE, mac = TRUE, clean = TRUE)
```
```
library(html2latex)
library(lme4)
library(sjPlot)
# This is a terrible model
model = lmer(mpg ~ cyl * disp + (1|vs), mtcars)
# We save the sjPlot table to an .html file
table = sjPlot::tab_model(model, file = "temp.html")
# Create tex
html2pdf(filename = "temp.html", table_width = 13, build_pdf = TRUE, silent = TRUE)
```
\newpage
## tab_model() html tables in a Rmd document
You can include tab_model() table in a Rmarkdown pdf in three steps:
### 1. YAML header
The YAML heather of the .Rmd document must include this:
```
header-includes:
- \usepackage{array}
- \usepackage{longtable}
- \newcommand\textstyleStrongEmphasis[1]{\textbf{#1}}
- \makeatletter
- \newcommand\arraybslash{\let\\\@arraycr}
```
### 2. Extract the table bit from the tex file
The tex file created with html2pdf can be rendered as a pdf by opening the tex file in Rstudio and using the `Compile PDF` button. But if you want to use the table code (from `\begin{longtable}` to `\end{longtable}`), we need to extract it first.
```{r message=FALSE, warning=FALSE}
# Create table.txt to be able to use it in Rmd documents
tex2Rmd("temp.tex")
# File with table code created in: table.txt
```
### 3. Use this code in the Rmd document.
Finally, you need to insert the latex code below outside of a chunk in your Rmd file.
```
\newcommand{\myinput}[1]{%
\begingroup%
\renewcommand\normalsize{\small}% Specify your font modification
\input{#1}%
\endgroup%
}
\begin{centering}
\myinput{table.txt}
\end{centering}
```
---
And the result will look like this:
\newcommand{\myinput}[1]{%
\begingroup%
\renewcommand\normalsize{\small}% Specify your font modification
\input{#1}%
\endgroup%
}
\begin{centering}
\myinput{table.txt}
\end{centering}
---
## Manually input latex code
Alternativelly, you can manually insert the contents of table.txt in a chunk staring with ` ```{=latex}`
See: https://bookdown.org/yihui/rmarkdown-cookbook/raw-latex.html