/
knitr.Rmd
297 lines (220 loc) · 5.48 KB
/
knitr.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
---
title: Use PRQL with knitr
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Use PRQL with knitr}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
Loading `{prqlr}` makes the `prql` code chunks executable in R Markdown and Quarto Markdown files.
## Output formats
It can either output the results of the actual query execution or display a SQL query compiled from a PRQL query.
### Use with `{DBI}` connections
If using with database connections (set the name of `{DBI}` connection to the `connection` chunk option),
PRQL code chunks pass the output SQL to `{knitr}`'s SQL engine and
behaves like [SQL code chunks](https://bookdown.org/yihui/rmarkdown/language-engines.html#sql).
So, usage is the same as for SQL code chunks.
For example, let's render an R Markdown file named `test.Rmd` with the following contents
with the `knitr::knit()` function.
````{r}
#| echo: false
#| results: asis
# Override the default SQL print function to avoid inserting '<div class="knitsql-table">'
# https://github.com/yihui/xaringan/issues/307
knitr::opts_knit$set(sql.print = \(x) paste(knitr::kable(x, format = "markdown"), collapse = "\n"))
src_with_con <- r"(
```{r}
#| echo: false
library(DBI)
library(prqlr)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "mtcars", mtcars)
```
```{prql}
#| connection: con
from mtcars
filter cyl > 6
select {cyl, mpg}
derive {mpg_int = math.round 0 mpg}
take 3
```
)"
cat("````markdown", src_with_con, "````", sep = "")
````
After rendering, a Markdown file named `test.md` is generated with the following contents.
````{r}
#| echo: false
#| results: asis
cat(
"````markdown",
knitr::knit_child(text = src_with_con, quiet = TRUE),
"````",
sep = ""
)
````
Note that the syntax highlighting of Elm is the best for PRQL,
so the output code blocks are marked as `elm`.
This can be changed by setting the `lang` chunk option.
### Without database connections
PRQL code chunks without the `connection` option will generate SQL code blocks.
````{r}
#| echo: false
#| results: asis
src_without_con <- r"(
```{r}
#| echo: false
library(prqlr)
```
```{prql}
from mtcars
filter cyl > 6
select {cyl, mpg}
derive {mpg_int = math.round 0 mpg}
take 3
```
)"
cat("````markdown", src_without_con, "````", sep = "")
````
The R Markdown file above will be converted to the Markdown file below.
````{r}
#| echo: false
#| results: asis
cat(
"````markdown",
knitr::knit_child(text = src_without_con, quiet = TRUE),
"````",
sep = ""
)
````
## Engine options
### Compiler options
We can pass some options of `prql_compile()` via the chunk option `engine-opts` (or `engine.opts`).
Note that the `format` option is always `TRUE` for PRQL code chunks.
````{r}
#| echo: false
#| results: asis
src_engine_opts <- r"(
```{r}
#| echo: false
library(prqlr)
```
## YAML-style
```{prql}
#| engine-opts:
#| target: sql.mssql
#| signature_comment: false
from mtcars
take 3
```
## R-style
```{prql engine.opts=list(target="sql.mssql", signature_comment=FALSE)}
from mtcars
take 3
```
)"
# Check if it can be rendered
invisible(knitr::knit_child(text = src_engine_opts, quiet = TRUE))
cat("````markdown", src_engine_opts, "````", sep = "")
````
### Parameterized PRQL code blocks
The experimental engine option `use_glue` can be used to insert R variables into PRQL queries.
Characters enclosed in `{{` and `}}` are replaced by the `glue::glue()` function
before being passed to the `prql_compile()` function.
````{r}
#| echo: false
#| results: asis
src_glue <- r"(
```{r}
#| echo: false
library(prqlr)
cyl_min <- 6
derive_or_select <- "derive"
```
```{prql}
#| engine-opts:
#| use_glue: true
from mtcars
filter cyl > {{cyl_min}}
select {cyl, mpg}
{{derive_or_select}} {mpg_int = math.round 0 mpg}
take 3
```
)"
cat("````markdown", src_glue, "````", sep = "")
````
The R Markdown file above will be converted to the Markdown file below.
````{r}
#| echo: false
#| results: asis
cat(
"````markdown",
knitr::knit_child(text = src_glue, quiet = TRUE),
"````",
sep = ""
)
````
#### Use query strings in R code blocks
With the experimental `use_glue` option, the query string can even be defined completely in R side.
````{r}
#| echo: false
#| results: asis
src_query_from_r <- r"(
```{r}
#| echo: false
library(prqlr)
prql_query <- "from mtcars
select cyl"
```
```{prql}
#| engine-opts:
#| use_glue: true
{{prql_query}}
```
)"
cat("````markdown", src_query_from_r, "````", sep = "")
````
This example outputs the following.
````{r}
#| echo: false
#| results: asis
cat(
"````markdown",
knitr::knit_child(text = src_query_from_r, quiet = TRUE),
"````",
sep = ""
)
````
### Set special info string to output SQL code blocks
When making SQL code blocks output without a DB connection,
the `info_string` option can be used to control the info string of the output code block.
This is convenient when we want to add special attributes that are used in Quarto Markdown files.
The default value is `"sql"`.
For example, the following R Markdown file includes a PRQL code block with the `info_string` option will be converted to...
````{r}
#| echo: false
#| results: asis
src_info_string <- r"(
```{r}
#| echo: false
library(prqlr)
```
```{prql}
#| engine-opts:
#| info_string: '{.sql filename="SQL"}'
from mtcars
take 3
```
)"
cat("````markdown", src_info_string, "````", sep = "")
````
This Markdown file.
````{r}
#| echo: false
#| results: asis
cat(
"````markdown",
knitr::knit_child(text = src_info_string, quiet = TRUE),
"````",
sep = ""
)
````