I am using tidyverse to connect to multiple databases with the same data structure (clusters). Due to different database sources a union ist not possible without copy locally.
I can do everything with long coding, but now I try to shorten the code an run in the following problem. When defining the column names for the select statement dbplyr stores that with a looping variable into the connection rather than evaluating and store the result of the string.
Here is a minimal reproducible example:
library(tidyverse)
#reproducable example with two database and two tables in memory
con1 <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
con2 <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
copy_to(con1, mtcars)
copy_to(con1, iris)
copy_to(con2, mtcars)
copy_to(con2, iris)
#names of the tables
tables<-c("mtcars", "iris")
#specify which columns to select from which table
columns<-list("mtcars"=c("mpg", "hp"),
"iris"=c("Sepal.Length", "Sepal.Width"))
#list to put
data_list<-vector(mode="list", length=length(tables))
names(data_list)<-tables
#loop over tables
for(i in tables){
#loop over databases
for(j in 1:2)
data_list[[i]][[j]]<-tbl(get(paste0("con",j)), i)%>%select(columns[[i]])
}
This code works fine so far, but the problem is with accessing the data stored in the list (data_list).
If I try
data_list[[1]][[1]]
R still evaluate
select(columns[[i]])
After looping i ist "iris" and the statement give the error message:
Error: Unknown columns Sepal.Length
and Sepal.Width
For the second list in data_list it works fine because i is set appropriate:
data_list[[2]][[1]]
How can I force the select statement to evaluate the expression and not to store the expression with the looping variable I?
In the next step, I would like to add a filter expression too so that I don't have to collect all the data and only the data needed.
If the union would work over databases without copy that would solve all the problems
Thx and best regards Thomas