You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.3KB

  1. prep_open_dataset <- function(path_prep, partitioning = "sboe_id", ...) {
  2. path_prep <- resolve_path_data_prep(path_prep)
  3. if (length(fs::dir_ls(path_prep, type = "dir")) == 0) {
  4. partitioning <- NULL
  5. }
  6. arrow::open_dataset(path_prep, partitioning = partitioning, ...)
  7. }
  8. prep_open_dataset_db <- function(table, ..., path_prep = table) {
  9. pq <- prep_open_dataset(path_prep, ...)
  10. con <- duckdb_global_con()
  11. duckdb::duckdb_register_arrow(con, table, pq)
  12. dplyr::tbl(con, table)
  13. }
  14. prep_open_address_db <- function(
  15. path_db = "address_lookup.sqlite"
  16. ) {
  17. path_db <- resolve_path_data_prep(path_db)
  18. con <- if (!is.null(.globals$con_address)) {
  19. .globals$con_address
  20. } else {
  21. .globals$con_address <- DBI::dbConnect(RSQLite::SQLite(), path_db)
  22. }
  23. tbl(con, "resolved")
  24. }
  25. # Utils ----
  26. resolve_path_data_prep <- function(path_prep) {
  27. if (fs::file_exists(path_prep)) {
  28. return(path_prep)
  29. }
  30. path_here <- here::here("data-prep/", path_prep)
  31. path_up <- fs::path("..", "data-prep", path_prep)
  32. path_up2 <- fs::path("..", "..", "data-prep", path_prep)
  33. if (fs::file_exists(path_here)) {
  34. return(path_here)
  35. }
  36. if (fs::file_exists(path_up)) {
  37. return(path_up)
  38. }
  39. if (fs::file_exists(path_up2)) {
  40. return(path_up2)
  41. }
  42. stop("File not found: ", path_prep)
  43. }