-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve functions so that they can save historical datasets
- Loading branch information
Showing
9 changed files
with
140 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,5 +28,7 @@ Suggests: | |
jsonlite, | ||
mockery, | ||
withr, | ||
httpcode | ||
httpcode, | ||
cli, | ||
stringr | ||
Config/testthat/edition: 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#' Save Polis Data to compressed RDS File | ||
#' | ||
#' This function saves a given POLIS data object to an RDS file using a | ||
#' specific naming convention based on the current date. It also manages the | ||
#' retention of only the 5 most recent datasets in the specified directory, | ||
#' removing older datasets if necessary. | ||
#' | ||
#' @param polis_data The POLIS data object to be saved. | ||
#' @param polis_path The directory path where the RDS file will be saved. This | ||
#' function will check this directory for existing datasets and will maintain | ||
#' only the 5 most recent datasets, deleting older ones. | ||
#' @param max_datasets The max number of datasets to retain in the directory. | ||
#' | ||
#' @return Invisible NULL. This function is used for its side effect of | ||
#' saving a file and potentially deleting older files, rather than for | ||
#' returning a value. | ||
#' | ||
#' @examples | ||
#' # Assume `polis_data` is your dataset and `./polis_datasets` is your | ||
#' # target directory | ||
#' # save_polis_data(polis_data, "./polis_datasets") | ||
#' | ||
#' @export | ||
save_polis_data <- function(polis_data, polis_path, | ||
filname, max_datasets = 5) { | ||
|
||
cli::cli_process_start("Saving POLIS data into a compressed RDS file.") | ||
|
||
# generate the file name based on the current date | ||
suffix_name <- sprintf("_%s.rds", format(Sys.Date(), "%Y_%V")) | ||
full_path <- file.path(polis_path, paste0(filname, suffix_name)) | ||
|
||
# save polis list | ||
saveRDS(polis_data, full_path, compress = "xz") | ||
|
||
cli::cli_process_done( ) | ||
|
||
# Check existing datasets and keep only the 5 most recent | ||
existing_files <- list.files(polis_path, full.names = TRUE) | ||
|
||
if (length(existing_files) > 5) { | ||
# Sort files by date, assuming the naming convention holds the date info | ||
file_dates <- sapply(existing_files, function(x) { | ||
as.Date(stringr::str_extract(x, "\\d{4}_\\d{2}"), "%Y_%V") | ||
}) | ||
|
||
oldest_files <- existing_files[order( | ||
file_dates)][1:(length(existing_files)-5)] | ||
|
||
cli::cli_alert_success( | ||
"Removing {length(oldest_files)} old file(s) to keep top {max_datasets}.") | ||
|
||
suppressMessages(file.remove(oldest_files)) | ||
} | ||
cli::cli_process_done( ) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters