learnr
learnr copied to clipboard
Best practices for specifying the database connection in SQL exercises
We should think through the best approach for using SQL exercises in learnr documents. Currently, the following small tutorial won't work
---
title: "SQL"
output: learnr::tutorial
runtime: shiny_prerendered
---
## Using SQL
```{r setup}
library(learnr)
```
```{sql sql-ex, connection = starwarsdb::starwars_connect(), exercise = TRUE}
select * from people;
```
because the connection
chunk option is evaluated and serialized into
connection = new("duckdb_connection", dbdir = character(0),
conn_ref = <pointer: 0x7fe936fc8080>, driver = new("duckdb_driver",
database_ref = <pointer: 0x7fe936f7a9f0>, dbdir = ":memory:",
read_only = FALSE), debug = FALSE)
which is broken by the <pointer: 0x7fe936fc8080>
.
When this happens, the only way is to create the connection in the global setup chunk and reference its object name as a character string. Even using the connection object name unquoted will cause it to be serialized.
---
title: "SQL"
output: learnr::tutorial
runtime: shiny_prerendered
---
## Using SQL
```{r setup}
library(learnr)
con <- starwarsdb::starwars_connect()
```
```{sql sql-ex, connection = "con", exercise = TRUE}
select * from people;
```
We might want to protect this option from serialization. I also wonder what would be the best approach for handling connection pooling or serving tutorials for many users at once from the same host.