generated from rpkgs/rpkg.template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
159 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,4 @@ | |
^INPUT$ | ||
^Figures$ | ||
^OUTPUT$ | ||
^inst$ | ||
^tidydb2\.Rproj$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#' @import R6 | ||
#' @importFrom duckdb duckdb duckdb_shutdown | ||
#' @export | ||
dbase <- R6Class("duckdb_base", list( | ||
db = NULL, | ||
table = NULL, | ||
type = "duckdb", | ||
con = NULL, | ||
tbl = NULL, | ||
initialize = function(db = NULL, table = NULL, type = c("duckdb", "sqlite")) { | ||
dbinfo <- get_dbInfo("duckdb")[[1]] # 默认是第一个 | ||
if (is.null(db)) db <- dbinfo$db | ||
self$db <- db | ||
|
||
# 从文件后缀能够猜出变量类型 | ||
type <- match.arg(type) | ||
self$type <- type | ||
|
||
if (type == "duckdb") { | ||
self$con <- dbConnect(duckdb(), dbdir = self$db, read_only = TRUE) | ||
} else if (type == "sqlite") { | ||
self$con <- dbConnect(duckdb(), dbdir = self$db, read_only = TRUE) | ||
} | ||
|
||
if (is.null(table)) table <- dbinfo$table[1] %||% DBI::dbListTables(self$con)[1] | ||
self$table <- table | ||
self$tbl <- tbl(self$con, self$table) | ||
}, | ||
print = function(...) { | ||
cat(sprintf("db : %s\n", self$db)) | ||
cat(sprintf("table: %s\n", self$table)) | ||
print(self$tbl) | ||
}, | ||
finalize = function() { | ||
message("close datadb ...") | ||
self$close(force = TRUE) | ||
}, | ||
close = function(force = FALSE) { | ||
if (!force) { | ||
DBI::dbDisconnect(self$con, shutdown = TRUE) | ||
} else { | ||
duckdb::duckdb_shutdown(duckdb()) | ||
} | ||
}, | ||
read_data = function(site_id = 50349L, verbose = TRUE) { | ||
suppressWarnings({ | ||
t <- system.time({ | ||
d <- self$tbl |> | ||
filter(site == site_id) |> | ||
collect() | ||
}) | ||
if (verbose) print(t) | ||
d | ||
}) | ||
} | ||
## add a write table options | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#' @export | ||
edit_db_config <- function() { | ||
f = normalizePath("~/.db.yml") | ||
usethis::edit_file(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
library(dplyr) | ||
library(duckdb) | ||
# dbinfo$hourly_2020_2022 | ||
|
||
db = dbase$new() | ||
d = db$read_data() # first time about 20s, 16s可以读进来所有数据 | ||
d <- db$read_data(50246) # 0.6s | ||
# db$close(force=TRUE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
```{r} | ||
library(duckdb) | ||
library(data.table) | ||
library(dplyr) | ||
db = "z:/DATA/China/ChinaMet_hourly_mete2000/data/China_Mete2000_hourly_full_2020-2022_tidy.duckdb" | ||
f = "z:/DATA/China/ChinaMet_hourly_mete2000/data/China_Mete2000_hourly_full_2020-2022_tidy.csv" | ||
``` | ||
|
||
# 写入数据 | ||
|
||
```{r} | ||
# Create a connection to DuckDB | ||
con <- dbConnect(duckdb::duckdb(db)) | ||
df = fread(f) | ||
# Import the CSV file into DuckDB | ||
duckdb::dbWriteTable(con, "China_Mete2000_hourly_2020_2022", df) | ||
# Close the connection | ||
dbDisconnect(con) | ||
``` | ||
|
||
# 读取数据 | ||
|
||
```{r} | ||
dbDisconnect(con, shutdown = TRUE) | ||
dbExecute(con, "CREATE INDEX idx_site ON 'China_Mete2000_hourly_full_2020-2022' (site)") | ||
dbDisconnect(con) | ||
``` | ||
|
||
```{r} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,5 +56,3 @@ dbinfo = get_dbInfo() # see which db to read | |
con_mysql = open_mysql() | ||
con_mariadb = open_mariadb(1, dbinfo) | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
title: "database_tidydata" | ||
output: rmarkdown::html_vignette | ||
vignette: > | ||
%\VignetteIndexEntry{database_tidydata} | ||
%\VignetteEngine{knitr::rmarkdown} | ||
%\VignetteEncoding{UTF-8} | ||
--- | ||
|
||
```{r, include = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#>" | ||
) | ||
``` | ||
|
||
## 测试读取和清洗数据 | ||
|
||
```{r setup} | ||
library(tidydb2) | ||
``` | ||
|
||
```{r} | ||
``` | ||
|