File: memdb.R

package info (click to toggle)
r-cran-dbplyr 2.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,376 kB
  • sloc: sh: 13; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 1,104 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#' Create a database table in temporary in-memory database.
#'
#' `memdb_frame()` works like [tibble::tibble()], but instead of creating a new
#' data frame in R, it creates a table in [src_memdb()].
#'
#' @inheritParams tibble::tibble
#' @param name,.name Name of table in database: defaults to a random name that's
#'   unlikely to conflict with an existing table.
#' @param df Data frame to copy
#' @export
#' @examples
#' library(dplyr)
#' df <- memdb_frame(x = runif(100), y = runif(100))
#' df %>% arrange(x)
#' df %>% arrange(x) %>% show_query()
#'
#' mtcars_db <- tbl_memdb(mtcars)
#' mtcars_db %>% group_by(cyl) %>% summarise(n = n()) %>% show_query()
memdb_frame <- function(..., .name = unique_table_name()) {
  x <- copy_to(src_memdb(), tibble(...), name = .name)
  x
}

#' @rdname memdb_frame
#' @export
tbl_memdb <- function(df, name = deparse(substitute(df))) {
  copy_to(src_memdb(), df, name = name) # nocov
}

#' @rdname memdb_frame
#' @export
src_memdb <- function() {
  cache_computation("src_memdb", {
    src_dbi(DBI::dbConnect(RSQLite::SQLite(), ":memory:", create = TRUE))
  })
}