【发布时间】:2020-12-24 21:13:49
【问题描述】:
请查看帖子末尾的简单脚本。 我有一个包含两个表的数据库,我使用 union_all 将它们组合在一起。 有没有办法在不收集数据的情况下将结果添加到数据库中,即将它们加载到内存中? 非常感谢!
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="db.sqlite")
dbWriteTable(con,"mydata1",df1, overwrite=T)
dbWriteTable(con,"mydata2",df2, overwrite=T)
dbDisconnect(con) # closes our DB connection
con <- dbConnect(drv=RSQLite::SQLite(), dbname="db.sqlite")
mydb1 <- tbl(con, "mydata1")
mydb2 <- tbl(con, "mydata2")
mydb12 <- union_all(mydb1,mydb2)
#is there a way to add the union of mydb1 and mydb2 to the database without explicitly collecting the data?
由reprex package (v0.3.0) 于 2020 年 12 月 24 日创建
【问题讨论】:
-
只需在数据库中运行
UNION ALL。