No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

67 líneas
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. con <- duckdb_global_con()
  23. duckdb::duckdb_register_arrow(con, table, pq)
  24. dplyr::tbl(con, table)
  25. }
  26. prep_open_address_db <- function(
  27. path_db = "address_lookup.sqlite"
  28. ) {
  29. path_db <- resolve_path_up_2(path_db)
  30. con <- if (!is.null(.globals$con_address)) {
  31. .globals$con_address
  32. } else {
  33. .globals$con_address <- DBI::dbConnect(RSQLite::SQLite(), path_db)
  34. }
  35. tbl(con, "resolved")
  36. }
  37. # Utils ----
  38. resolve_path_up_2 <- function(path, dir = "data-prep") {
  39. if (fs::file_exists(path)) {
  40. return(path)
  41. }
  42. path_here <- here::here(dir, path)
  43. path_wd <- fs::path(dir, path)
  44. path_up <- fs::path("..", dir, path)
  45. path_up2 <- fs::path("..", "..", dir, path)
  46. if (fs::file_exists(path_wd)) return(path_wd)
  47. if (fs::file_exists(path_here)) return(path_here)
  48. if (fs::file_exists(path_up)) return(path_up)
  49. if (fs::file_exists(path_up2)) return(path_up2)
  50. stop("File not found: ", path)
  51. }