Skip to content

Instantly share code, notes, and snippets.

@iangow
Created January 13, 2024 16:35
Show Gist options
  • Save iangow/e907d7543a05b8a713d8ea2030806b22 to your computer and use it in GitHub Desktop.
Save iangow/e907d7543a05b8a713d8ea2030806b22 to your computer and use it in GitHub Desktop.
Illustrate `rows_append()`.
library(dplyr, warn.conflicts = FALSE)
library(DBI)
set.seed(1)
make_dat <- function(nrow=1, ncol=5) {
  df <- 
    cbind.data.frame(name=stringi::stri_rand_strings(n=nrow, length=3), 
                     matrix(data=sample(c(T, F), nrow*ncol, T), ncol=ncol))
  names(df) <- c("name", paste0("v_", 1:ncol))
  df
                  
}
my_data_base <- make_dat(nrow=10, ncol=5)

new_data <- make_dat(nrow=1, ncol=5)


db <- dbConnect(duckdb::duckdb())

old_data_db <- copy_to(db, my_data_base, name = "old_data")
old_data_db |> count()
#> # Source:   SQL [1 x 1]
#> # Database: DuckDB v0.9.2 [root@Darwin 23.3.0:R 4.3.2/:memory:]
#>       n
#>   <dbl>
#> 1    10

new_data_db <- copy_to(db, new_data, name = "new_data")

to_write <-
  new_data_db |>
  anti_join(old_data_db)
#> Joining with `by = join_by(name, v_1, v_2, v_3, v_4, v_5)`

rows_append(old_data_db, to_write, in_place = TRUE)
old_data_db |> count()
#> # Source:   SQL [1 x 1]
#> # Database: DuckDB v0.9.2 [root@Darwin 23.3.0:R 4.3.2/:memory:]
#>       n
#>   <dbl>
#> 1    11

Created on 2024-01-13 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment