Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make interlace a S3 generic #113

Merged
merged 7 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ S3method(get_tables,SQLiteConnection)
S3method(id,Id)
S3method(id,character)
S3method(id,tbl_dbi)
S3method(interlace,tbl_sql)
S3method(schema_exists,DBIConnection)
S3method(schema_exists,SQLiteConnection)
S3method(schema_exists,default)
Expand All @@ -53,7 +54,7 @@ export(get_schema)
export(get_table)
export(get_tables)
export(id)
export(interlace_sql)
export(interlace)
export(is.historical)
export(nrow)
export(schema_exists)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

* The `%notin%` operator has been removed from the package (#??).

* The `interlace_sql()` function is converted to the S3 generic `interlace()` (#113).

## New features

* The S3 method `as.character.Id()` is added which converts `DBI::Id()` to `character` (#93).
Expand Down
36 changes: 19 additions & 17 deletions R/interlace_sql.R → R/interlace.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Combine any number of SQL queries, where each has their own time axis of
#' Combine any number of tables, where each has their own time axis of
#' validity (valid_from and valid_until)
#'
#' @description
Expand All @@ -15,39 +15,41 @@
#' colnames must be named in same order as as given in tables
#' (i.e. t1, t2, t3, ...).
#' @examples
#' conn <- get_connection(drv = RSQLite::SQLite())
#' conn <- get_connection(drv = RSQLite::SQLite())
#'
#'
#' if (schema_exists(conn, "test")) {
#' t1 <- data.frame(key = c("A", "A", "B"),
#' obs_1 = c(1, 2, 2),
#' valid_from = as.Date(c("2021-01-01", "2021-02-01", "2021-01-01")),
#' valid_until = as.Date(c("2021-02-01", "2021-03-01", NA)))
#' t1 <- dplyr::copy_to(conn, t1, id("test.SCDB_tmp1", conn), overwrite = TRUE, temporary = FALSE)
#' obs_1 = c(1, 2, 2),
#' valid_from = as.Date(c("2021-01-01", "2021-02-01", "2021-01-01")),
#' valid_until = as.Date(c("2021-02-01", "2021-03-01", NA))) |>
#' dplyr::copy_to(conn, df = _, name = "t1")
#'
#' t2 <- data.frame(key = c("A", "B"),
#' obs_2 = c("a", "b"),
#' valid_from = as.Date(c("2021-01-01", "2021-01-01")),
#' valid_until = as.Date(c("2021-04-01", NA)))
#' t2 <- dplyr::copy_to(conn, t2, id("test.SCDB_tmp2", conn), overwrite = TRUE, temporary = FALSE)
#' obs_2 = c("a", "b"),
#' valid_from = as.Date(c("2021-01-01", "2021-01-01")),
#' valid_until = as.Date(c("2021-04-01", NA))) |>
#' dplyr::copy_to(conn, df = _, name = "t2")
#'
#' interlace_sql(list(t1, t2), by = "key")
#' }
#' interlace(list(t1, t2), by = "key")
#'
#' close_connection(conn)
#' close_connection(conn)
#' @return The combination of input queries with a single, interlaced
#' valid_from / valid_until time axis
#' @importFrom rlang .data
#' @export
interlace_sql <- function(tables, by = NULL, colnames = NULL) {

interlace <- function(tables, by = NULL, colnames = NULL) {
# Check arguments
checkmate::assert_character(by)
# TODO: how to checkmate tables and colnames?

# Check edgecase
if (length(tables) == 1) return(purrr::pluck(tables, 1))

UseMethod("interlace", tables[[1]])
}

#' @export
interlace.tbl_sql <- function(tables, by = NULL, colnames = NULL) {

# Parse inputs for colnames .from / .to columns
from_cols <- seq_along(tables) |>
purrr::map_chr(\(t) paste0("t", t, ".from")) |>
Expand Down
35 changes: 16 additions & 19 deletions man/interlace_sql.Rd → man/interlace.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_that("interlace_sql() works", {
test_that("interlace.tbl_sql() works", {
for (conn in get_test_conns()) {

t1 <- data.frame(key = c("A", "A", "B"),
Expand Down Expand Up @@ -34,17 +34,17 @@ test_that("interlace_sql() works", {
)


expect_identical(interlace_sql(list(t1, t2), by = "key") |> dplyr::collect(),
expect_identical(interlace(list(t1, t2), by = "key") |> dplyr::collect(),
t_ref |> dplyr::collect())

expect_mapequal(interlace_sql(list(t1, t2), by = "key") |> dplyr::collect(),
interlace_sql(list(t2, t1), by = "key") |> dplyr::collect())
expect_mapequal(interlace(list(t1, t2), by = "key") |> dplyr::collect(),
interlace(list(t2, t1), by = "key") |> dplyr::collect())

connection_clean_up(conn)
}
})


test_that("interlace_sql returns early if length(table) == 1", {
expect_identical(mtcars$mpg, interlace_sql(mtcars["mpg"], by = "mpg"))
test_that("interlace returns early if length(table) == 1", {
expect_identical(mtcars$mpg, interlace(mtcars["mpg"], by = "mpg"))
})
Loading