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

66 lines
1.8KB

  1. process_expenditures_csv <- function(dir_sboe_id, report_list = tar_read(report_list)) {
  2. # Read the files in the directory, extract report_id from the path
  3. # Compare to report_list to determine which reports go into the data
  4. files <- dir_ls(dir_sboe_id)
  5. info <- report_path_info(files)
  6. info$path <- files
  7. # These are the reports we want to keep in the data
  8. info <- semi_join(info, report_list, by = c("sboe_id", "report_id"))
  9. expenditures <-
  10. info |>
  11. pmap(function(sboe_id, report_id, path, ...) {
  12. read_expenditures_csv(path, sboe_id, report_id)
  13. }) |>
  14. list_rbind()
  15. names(expenditures) <- snakecase::to_snake_case(names(expenditures), parsing_option = 3)
  16. expenditures
  17. }
  18. write_expenditures_parquet <- function(dir_sboe_id, report_list = tar_read(report_list)) {
  19. expenditures <- process_expenditures_csv(dir_sboe_id, report_list)
  20. info <- report_path_info(dir_sboe_id)
  21. data_dir <- here::here("..", "data", "expenditures", sprintf("sboe_id=%s", info$sboe_id))
  22. data_path <- path(data_dir, "part-0.parquet")
  23. dir_create(data_dir)
  24. arrow::write_parquet(expenditures, data_path)
  25. data_path
  26. }
  27. read_expenditures_csv <- function(path, sboe_id = NULL, report_id = NULL) {
  28. if (file_size(path) < 1) {
  29. return(NULL)
  30. }
  31. if (is.null(sboe_id) || is.null(report_id)) {
  32. info <- report_path_info(path)
  33. sboe_id <- info$sboe_id
  34. report_id <- info$report_id
  35. }
  36. x <- read_csv(
  37. path,
  38. col_types = cols(
  39. .default = col_character(),
  40. OccurDate = col_date("%m/%d/%Y"),
  41. IsOrg = col_logical(),
  42. IsUS = col_logical(),
  43. Amount = col_double(),
  44. SumToDate = col_double(),
  45. IsAggregated = col_logical()
  46. )
  47. )
  48. record_problems(x, label = "expenditures")
  49. x |>
  50. mutate(sboe_id = sboe_id, report_id = report_id, .before = 0)
  51. }