在 Rnotebook 中为 SQL 块设置全局数据库连接

Set Global Database Connection for SQL chunk in Rnotebook

有没有办法在 Rnotebook 中设置全局数据库连接,这样您就不必为每个 SQL 块设置数据库连接?

目前我必须为每个SQL块设置如下:

```{sql connection = my_connection}
select * from my_table
```

我希望能够做到以下几点:

```{sql}
select * from my_table
```

来自Rmarkdown docs

Setting a Default Connection

If you have many SQL chunks, it may be helpful to set a default for the connection chunk option in the setup chunk, so that it is not necessary to specify the connection on each individual chunk. You can do this as follows:

```{r setup}
library(DBI)
db <- dbConnect(RSQLite::SQLite(), dbname = "sql.sqlite")
knitr::opts_chunk$set(connection = "db")
```

因此,在您的示例中,设置一个挂钩

```{r setup}
# insert your my_connection declaration here
knitr::opts_chunk$set(connection = "my_connection")
``` 

并将该块放在所有其他块之前。