1

在 RI 中使用数据库观察到以下情况:

library(dplyr)
library(RSQLite)
library(dbplyr)

## Creating data
d <- data.frame(x=1:100)

## Setting up a sqlite database
dv <- dbDriver("SQLite")
con <- dbConnect(dv, dbname="test.db")

## Writing the data to the db
dbWriteTable(con, "data", d,header=T)

## Reading the data back
d.tbl = tbl(con,"data" )

## Apply a filter and collect the data
d.tbl <- d.tbl %>% filter(x<5)
d.coll <- collect(d.tbl)

## Save the filtered data into another database
con2 <- dbConnect(dv, dbname="test2.db")
dbWriteTable(con2, "data", d.coll, header=T)

## Reading it again to make sure the filter was applied
dx1 <- dbReadTable(con, "data")
dx2 <- dbReadTable(con2, "data")

奇迹般有效。查看生成的两个 db 文件,我看到:

-rw-r--r--   1 mycomp  staff        2048 19 Oct 23:24 test.db
-rw-r--r--   1 mycomp  staff        2048 19 Oct 23:24 test2.db

它们具有完全相同的大小......即使 test2.db 包含完整数据和查询,除了 test.db 之外,它至少还有查询。我确保已经应用了过滤器。

有什么启示吗?

4

0 回答 0