-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDF_Example.Rmd
173 lines (125 loc) · 3.35 KB
/
PDF_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
---
output:
pdf_document:
latex_engine: lualatex
includes:
in_header: "template.tex"
fontsize: 12pt
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(scales))
suppressPackageStartupMessages(library(kableExtra))
suppressPackageStartupMessages(library(formattable))
```
## PDF Example
This is an example of an RMarkdown file that will render to PDF.
While RMarkdown will translate to Latex, you can incorporate code in the document as well. `**Bolded Text**` and `\textbf{Bolded Text}` will both result in **Bolded Text**.
## Lists
Here is an example of a list using RMarkdown.
`* Item 1`
`* Item 2`
`* Item 3`
renders as:
* Item 1
* Item 2
* Item 3
## Images
``
will insert this image:

You can also adjust the size of the image.
`{width=250px}`
{width=125px}
## R Code
```{r echo=TRUE}
df <- read_csv("data/sample_data.csv")
```
### Some data cleaning
```{r echo=TRUE}
levels <- c ("1 - Not at All",
"2",
"3",
"4",
"5 - Significantly")
df <- df %>%
mutate_at(vars (starts_with("session")),
~(parse_factor(., levels = levels)
)
)
```
## Charts
```{r echo = TRUE}
chart <- df %>%
count (session_useful) %>%
mutate (perc = n / sum (n)) %>%
ggplot (aes (session_useful, perc)) +
geom_col() +
scale_y_continuous(labels = scales::percent_format(accuracy = 1),
limits = c(0,0.5)) +
geom_text (aes (label = scales::percent(perc)),
vjust = -1) +
labs (x = "I found the session useful",
y = "Percent")
chart
```
{height=180px}
```{r echo=FALSE, fig.width=3}
chart
```
{height=180px}
```{r echo=FALSE, fig.width=8, fig.height=2}
chart
```
## Tables
### Regular R output
```{r echo=TRUE}
table <- df %>%
count (session_useful) %>%
rename (`I found the session useful` = session_useful) %>%
mutate (Percent = scales::percent (n / sum (n)) )
table
```
### Using the `kableExtra` package
```{r echo=TRUE}
table %>%
kable()
```
### Formatting with `kableExtra`
#### http://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
Changing the font size
```{r echo=TRUE}
table %>%
kable() %>%
kable_styling(position = "center", font_size = 15)
```
Adjusting column widths
```{r echo=TRUE, results='asis'}
table %>%
kable(align = c ("lcc")) %>%
kable_styling(font_size = 15) %>%
column_spec(2:3, width = "8em")
```
Formatting cells
```{r echo=TRUE}
table %>%
mutate (Percent = cell_spec (Percent,
background = "#FA19C3"),
n = ifelse (n > 40,
cell_spec (n, color = "green"),
cell_spec (n, color = "red"))) %>%
kable(escape = FALSE,
align = c ("lcc")) %>%
kable_styling(font_size = 15) %>%
column_spec(2:3, width = "8em")
```
## Columns using `minipage`
### Have to use `\includegraphics{path/to/image}` within `minipage`
\begin{minipage}[]{0.3\textwidth}
Here is some text.
\end{minipage}
\begin{minipage}[]{0.7\textwidth}
Here is some more text, and an image.\\
\includegraphics[width = 2cm]{images/hex-rmarkdown}
\end{minipage}