Skip to content

Commit

Permalink
Done check function
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelso committed Nov 13, 2024
1 parent b02d0bd commit ca460a5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
54 changes: 45 additions & 9 deletions R/fct_check_data2.R
Original file line number Diff line number Diff line change
Expand Up @@ -262,21 +262,57 @@ fct_check_data2 <- function(.usr, .time, .ad, .cs, .checklist){
## 6. Check matching and logical interactions

## - Period matching exactly between tables
tmp$match_period_ad_ok <- all(sort(unique(.ad$trans_period)) == sort(unique(.time$period_no)))

## -
cs_period <- .cs |> dplyr::filter(c_period != "ALL")
if (nrow(cs_period) > 0) {
tmp$match_period_cs_ok <- all(sort(unique(.cs$c_period)) == sort(unique(.time$period_no)))
} else {
tmp$match_period_cs_ok <- TRUE
}

## - LU matching between tables
lu_ad <- sort(c(unique(.ad$lu_initial_id), unique(.ad$lu_final_id)))
lu_cs <- sort(unique(.cs$lu_id))

tmp$match_lu_ok <- all(lu_ad == lu_cs)

## - DG method
## - At least one ref and one monitoring period
nb_ref <- .time |> dplyr::filter(stringr::str_detect(period_type, pattern = "REF|REF[0-9]"))
nb_mon <- .time |> dplyr::filter(stringr::str_detect(period_type, pattern = "MON|MON[0-9]"))

tmp$match_ref_ok <- nrow(nb_ref) > 0
tmp$match_mon_ok <- nrow(nb_mon) > 0

## - CF if DM
## TBD should add carbon fraction as row in c_stocks instead of user_inputs

# nb_ref <- .time |> dplyr::filter(stringr::str_detect(period_type, pattern = "REF|REF[0-9]"))
# nb_mon <- .time |> dplyr::filter(stringr::str_detect(period_type, pattern = "MON|MON[0-9]"))
## - DG method: either (1) dg_ratio applied to all pools, (2) dg_ratio applied to some pools other kept intact (dg_expool == T), (3) diff in Cstocks.
## NOT IMPLEMENTED YET

## - Combine
out$matches_ok <- all(
tmp$match_period_ad_ok,
tmp$match_period_cs_ok,
tmp$match_lu_ok,
tmp$match_ref_ok,
tmp$match_mon_ok
)

out$matches_ok <- all()
if (out$matches_ok) {
out$ids_pb <- NULL
} else {
if (tmp$match_period_ad_ok) tmp$match_period_ad_pb <- NULL else tmp$match_period_ad_pb <- "period_no != trans_period"
if (tmp$match_period_cs_ok) tmp$match_period_cs_pb <- NULL else tmp$match_period_cs_pb <- "period_no != c_period"
if (tmp$match_lu_ok) tmp$match_lu_pb <- NULL else tmp$match_lu_pb <- "land use mismatch between AD and CS"
if (tmp$match_ref_ok) tmp$match_ref_pb <- NULL else tmp$match_ref_pb <- "missing REF in time_periods"
if (tmp$match_mon_ok) tmp$match_mon_pb <- NULL else tmp$match_mon_pb <- "missing MON in time_periods"

out$matches_pb <- c(tmp$match_period_ad_pb, tmp$match_period_cs_pb, tmp$match_lu_pb, tmp$match_ref_pb, tmp$match_mon_pb)

## - combine all
}

## 7. combine all
out$all_ok <- all(
out$cols_ok,
out$size_ok,
Expand All @@ -286,9 +322,9 @@ fct_check_data2 <- function(.usr, .time, .ad, .cs, .checklist){
out$matches_ok
)

## Return results as 1st level elements for checks and 2nd level for problems
checks <- out[stringr::str_detect(names(out), pattern = "_ok")]
pbs <- out[stringr::str_detect(names(out), pattern = "_pb")]

list(checks = checks, pbs = pbs)
checks$pbs <- out[stringr::str_detect(names(out), pattern = "_pb")]
checks

}
2 changes: 1 addition & 1 deletion R/mod_tool_UI.R
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ mod_tool_UI <- function(id, i18n){
id = ns("check_cards"),
# layout_column_wrap(
# width = "200px",
card_cols, card_size, card_datatypes
card_cols, card_size, card_datatypes, card_cats, card_ids, card_matches
# )
))

Expand Down
16 changes: 9 additions & 7 deletions R/mod_tool_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod_tool_server <- function(id, rv) {
## Show hide data ok if tabs are correct

observe({
req(rv$inputs$xlsx_tabs)
req(rv$inputs$xlsx_tabs_ok)

if(rv$inputs$xlsx_tabs_ok) {
shinyjs::hide("msg_no_data")
Expand Down Expand Up @@ -89,12 +89,14 @@ mod_tool_server <- function(id, rv) {

## ++ Run checks ---------------------------------------------------------


## !!! NEED TO REVISE CHECK FUNCTION TO INCLUDE ALL CHECKS
# rv$check$check_data_ok <- fct_check_data(.ad = rv$inputs$ad, .cs = rv$inputs$cs, .init = rv$checklist)

## +++ Recap all checks ----
rv$checks$all_ok <- TRUE
## Use fct_check_data2()
rv$check$check_data_ok <- fct_check_data2(
.usr = rv$inputs$usr,
.time = rv$inputs$time,
.ad = rv$inputs$ad,
.cs = rv$inputs$cs,
.checklist = rv$checklist
)

Sys.sleep(0.1)

Expand Down

0 comments on commit ca460a5

Please sign in to comment.