Skip to content

Commit

Permalink
Merge pull request #72 from PIP-Technical-Team/sign_and_save
Browse files Browse the repository at this point in the history
update sign and save function
  • Loading branch information
randrescastaneda authored Feb 24, 2022
2 parents 8b1fb3c + 7a4de39 commit cc4cfa2
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 15 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: pipaux
Title: Management of auxiliary data in PIP workflow
Version: 0.0.4
Version: 0.0.4.9001
Authors@R: c(
person(given = "R.Andres",
family = "Castaneda",
Expand Down Expand Up @@ -45,7 +45,8 @@ Imports:
cli,
pipload (>= 0.1.4.9002),
tidyfast,
yaml
yaml,
qs
VignetteBuilder: knitr
Remotes:
github::PIP-Technical-Team/pipload@development
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export(pip_cpi)
export(pip_gdm)
export(pip_gdp)
export(pip_gdp_weo)
export(pip_income_groups)
export(pip_indicators)
export(pip_maddison)
export(pip_metadata)
Expand All @@ -20,6 +21,7 @@ export(pip_pop)
export(pip_ppp)
export(pip_prices)
export(pip_regions)
export(pip_sign_save)
export(pip_update_all_aux)
export(update_aux)
import(data.table)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# pipaux (development version)

# pipaux 0.0.4

* fix bugs on installation
Expand Down
52 changes: 40 additions & 12 deletions R/pip_sign_save.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
#' @param msrdir character: Directory where the data and data signature will be saved.
#' @param save_dta logical: If TRUE a Stata (.dta) version of the dataset is also saved.
#' @keywords internal
#' @export
pip_sign_save <- function(x,
measure,
msrdir,
force,
force = FALSE,
save_dta = TRUE) {

# Note: clean CPI data file and then create data signature
Expand All @@ -19,24 +20,35 @@ pip_sign_save <- function(x,
# check signature of current fst file
ds_production_path <- fs::path(msrdir, paste0(measure, "_datasignature.txt")) # data signature in production

if (file.exists(ds_production_path)) {
if (fs::file_exists(ds_production_path)) {

# read data signature in production
ds_production <- readr::read_lines(ds_production_path)[[1]]
} else {

# fake signature
ds_production <- "0000"
ms_status <- "new"
}

#--------- if Signature from dlw is different from the one in production ---------

if (ds_dlw != ds_production || force == TRUE) {
if (force == TRUE) {
ms_status <- "forced"
}

if (ds_dlw != ds_production) {
ms_status <- "changed"
} else {
ms_status <- "unchanged"
}

if (ms_status %in% c("forced", "changed")) {

# make sure directory exists
wholedir <- fs::path(msrdir, "_vintage/")
if (!(dir.exists(wholedir))) {
dir.create(wholedir, recursive = TRUE)
if (!(fs::dir_exists(wholedir))) {
fs::dir_delete(wholedir)
}

# re-write x in production if data signature is not found
Expand All @@ -48,6 +60,12 @@ pip_sign_save <- function(x,
x = x,
path = fs::path(msrdir, measure, ext = "fst")
)

qs::qsave(
x = x,
file = fs::path(msrdir, measure, ext = "qs")
)

if (save_dta) {
haven::write_dta(
data = x,
Expand All @@ -59,6 +77,10 @@ pip_sign_save <- function(x,
x = x,
path = fs::path(msrdir, "_vintage/", paste0(measure, "_", time), ext = "fst")
)
qs::qsave(
x = x,
file = fs::path(msrdir, "_vintage/", paste0(measure, "_", time), ext = "qs")
)
if (save_dta) {
haven::write_dta(
data = x,
Expand All @@ -74,16 +96,22 @@ pip_sign_save <- function(x,
file = ds_production_path
)

infmsg <- paste(
"Data signature has changed, it was not found,",
"or update was forced.\n",
paste0("`", measure, ".fst` has been updated")
)

rlang::inform(infmsg)
fillintext <- fcase(ms_status == "new", "was not found",
ms_status == "forced", "has been changed forcefully",
ms_status == "changed", "has changed",
default = "")


infmsg <-
"Data signature {fillintext}
{.file {measure}.fst} has been updated"

cli::cli_alert_warning(infmsg)
return(invisible(TRUE))
} else {
rlang::inform("Data signature is up to date.\nNo update performed")
cli::cli_alert_info("Data signature is up to date.
{cli::col_blue('No update performed')}")
return(invisible(FALSE))
}
}
2 changes: 2 additions & 0 deletions R/pipaux.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#' pipaux: A package for computating the notorious bar statistic
#'
#' Description bla blab
#'
#' @section pipaux functions:
#' The pipaux functions ...
#'
Expand Down
19 changes: 19 additions & 0 deletions man/pip_income_groups.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/pip_sign_save.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/pipaux.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/test-update_aux.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test_that("aux data is up to date", {
skip_on_ci()
skip('skip')
lf <- lsf.str("package:pipaux", pattern = "^pip_[a-z]{3}$")
lf <- as.character(lf)

Expand Down

0 comments on commit cc4cfa2

Please sign in to comment.