For reasons I am not going to explain here, I need to use the same connection object to read two databases in dbplyr. I found some online resources, but I am not getting this right. Please have a look at the reprex below. Can anyone tell me what I am doing wrong? Many thanks!
library(tidyverse)
library(DBI) # main DB interface
library(dbplyr) # dplyr back-end for DBs
#>
#> Attaching package: 'dbplyr'
#> The following objects are masked from 'package:dplyr':
#>
#> ident, sql
library(RSQLite)
##create the databases
df1 <- tibble(x=1:20,y=rep(c("a", "b"), 10))
df2 <- tibble(x=101:120,y=rep(c("d", "e"), 10))
con <- dbConnect(drv=RSQLite::SQLite(), dbname="db1.sqlite")
dbWriteTable(con,"mydata1",df1, overwrite=T)
dbDisconnect(con) # closes our DB connection
con <- dbConnect(drv=RSQLite::SQLite(), dbname="db2.sqlite")
dbWriteTable(con,"mydata2",df2, overwrite=T)
dbDisconnect(con) # closes our DB connection
## Now that I have created the two databases, I try reading them with the same connection object
con <- dbConnect(drv=RSQLite::SQLite())
db1 <- tbl(con, in_schema("db1.sqlite","mydata1"))
#> Error: no such table: db1.sqlite.mydata1
db2 <- tbl(con, in_schema("db2.sqlite","mydata2"))
#> Error: no such table: db2.sqlite.mydata2
### but this fails miserably. How to fix it?
Created on 2020-12-24 by the reprex package (v0.3.0)