如何在 R 中为 RSQLite 创建一个良好的可重现 SQL 数据库示例
How to make a good reproducible SQL database example for RSQLite in R
我需要询问有关 RSQLite
的问题,但无法共享我的数据库。我怎样才能在 R 中制作一个好的可重现的 SQLite 数据库示例?
说一个table这个形状:
df<-data.frame(
date=as.POSIXct(c("12/11/2019 12:00","12/11/2019 12:01","12/11/2019 12:01"),format="%d/%m/%Y %H:%M"),
category=c("Plant","Plant","Animal"),
value=c(1,2,3)
)
df
看起来像:
> df
date category value
1 2019-11-12 12:00:00 Plant 1
2 2019-11-12 12:01:00 Plant 2
3 2019-11-12 12:01:00 Animal 3
SQLite 没有正确的日期时间类型,因此必须以文本形式输入日期。
library(DBI)
library(dplyr)
tb <- dplyr::tibble(
date=c("12/11/2019 12:00","12/11/2019 12:01","12/11/2019 12:01"),
category=c("Plant","Plant","Animal"),
value=c(1,2,3)
)
mydb <- DBI::dbConnect(RSQLite::SQLite(), "")
DBI::dbWriteTable(mydb, "table1", tb, overwrite=T)
### Filtering the table
tb_sqlite <- tbl(mydb, "table1")
tb_sqlite
给出:
> tb_sqlite
# Source: table<table1> [?? x 3]
# Database: sqlite 3.29.0 []
date category value
<chr> <chr> <dbl>
1 12/11/2019 12:00 Plant 1
2 12/11/2019 12:01 Plant 2
3 12/11/2019 12:01 Animal 3
别忘了断开 table:
dbDisconnect(mydb)
我需要询问有关 RSQLite
的问题,但无法共享我的数据库。我怎样才能在 R 中制作一个好的可重现的 SQLite 数据库示例?
说一个table这个形状:
df<-data.frame(
date=as.POSIXct(c("12/11/2019 12:00","12/11/2019 12:01","12/11/2019 12:01"),format="%d/%m/%Y %H:%M"),
category=c("Plant","Plant","Animal"),
value=c(1,2,3)
)
df
看起来像:
> df
date category value
1 2019-11-12 12:00:00 Plant 1
2 2019-11-12 12:01:00 Plant 2
3 2019-11-12 12:01:00 Animal 3
SQLite 没有正确的日期时间类型,因此必须以文本形式输入日期。
library(DBI)
library(dplyr)
tb <- dplyr::tibble(
date=c("12/11/2019 12:00","12/11/2019 12:01","12/11/2019 12:01"),
category=c("Plant","Plant","Animal"),
value=c(1,2,3)
)
mydb <- DBI::dbConnect(RSQLite::SQLite(), "")
DBI::dbWriteTable(mydb, "table1", tb, overwrite=T)
### Filtering the table
tb_sqlite <- tbl(mydb, "table1")
tb_sqlite
给出:
> tb_sqlite
# Source: table<table1> [?? x 3]
# Database: sqlite 3.29.0 []
date category value
<chr> <chr> <dbl>
1 12/11/2019 12:00 Plant 1
2 12/11/2019 12:01 Plant 2
3 12/11/2019 12:01 Animal 3
别忘了断开 table:
dbDisconnect(mydb)