Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

69 lines
1.9KB

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