gpt4 book ai didi

r - 在 Rnotebook 中为 SQL block 设置全局数据库连接

转载 作者:行者123 更新时间:2023-12-02 00:42:31 24 4
gpt4 key购买 nike

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

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

```{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")
```

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

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

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

关于r - 在 Rnotebook 中为 SQL block 设置全局数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45774168/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com