From 7a006f6d6be5430c52d635c408e0bf620376ac03 Mon Sep 17 00:00:00 2001 From: madeliri Date: Mon, 20 Apr 2026 16:38:14 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20?= =?UTF-8?q?=D0=B2=20=D0=B2=D0=B8=D0=B4=D0=B5=20=D0=BC=D0=BE=D0=B4=D1=83?= =?UTF-8?q?=D0=BB=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.R | 3 +- modules/data_validation.R | 116 ++++++++++++++++++++------------------ modules/db.R | 3 - modules/global_options.R | 4 +- 4 files changed, 67 insertions(+), 59 deletions(-) diff --git a/app.R b/app.R index 59557d3..21a2448 100644 --- a/app.R +++ b/app.R @@ -31,6 +31,7 @@ global_options$set_global_options( shiny.host = "0.0.0.0" # enabled_schemes = "example_of_scheme" ) +global_options$check_and_init_scheme() # CHECK FOR PANDOC # TEMP ! NEED TO HANDLE @@ -1186,7 +1187,7 @@ server <- function(input, output, session) { "exporting data to xlsx", "importing data from xlsx" ), - key = NULL, + key = NA, con ) { diff --git a/modules/data_validation.R b/modules/data_validation.R index d3616ad..ab1a22b 100644 --- a/modules/data_validation.R +++ b/modules/data_validation.R @@ -1,14 +1,13 @@ +options(box.path = here::here()) +box::use(modules/data_manipulations[is_this_empty_value]) #' @export init_val = function(scheme, ns) { - options(box.path = here::here()) - box::use(modules/data_manipulations[is_this_empty_value]) - iv <- shinyvalidate::InputValidator$new() # если передана функция с пространством имен, то происходит модификация id - if(!missing(ns)) { + if (!missing(ns)) { scheme <- scheme |> dplyr::mutate(form_id = ns(form_id)) } @@ -37,18 +36,7 @@ init_val = function(scheme, ns) { # value `0; 250` -> transform to rule validation value from 0 to 250 if (form_type == "number") { - iv$add_rule(x_input_id, function(x) { - # exit if empty - if (is_this_empty_value(x)) { - return(NULL) - } - # хак для пропуска значений - if (x == "NA") return(NULL) - - # check for numeric - # if (grepl("^[-]?(\\d*\\,\\d+|\\d+\\,\\d*|\\d+)$", x)) NULL else "Значение должно быть числом." - if (grepl("^[+-]?\\d*[\\.|\\,]?\\d+$", x)) NULL else "Значение должно быть числом." - }) + iv$add_rule(x_input_id, val_is_a_number) # проверка на соответствие диапазону значений if (!is.na(choices)) { @@ -59,51 +47,15 @@ init_val = function(scheme, ns) { if (length(ranges) > 3) { warning("Количество переданных элементов'", x_input_id, "' > 2") } else { - iv$add_rule( - x_input_id, - function(x) { - - # exit if empty - if (is_this_empty_value(x)) { - return(NULL) - } - - if (x == "NA") return(NULL) - - # замена разделителя десятичных цифр - x <- stringr::str_replace(x, ",", ".") - - # check for currect value - if (dplyr::between(as.double(x), ranges[1], ranges[2])) { - NULL - } else { - glue::glue("Значение должно быть между {ranges[1]} и {ranges[2]}.") - } - } - ) + iv$add_rule(x_input_id, val_number_within_a_range, ranges = ranges) } } } if (form_type %in% c("select_multiple", "select_one", "radio", "checkbox")) { - iv$add_rule(x_input_id, function(x) { - - if (length(x) == 1) { - if (is_this_empty_value(x)) return(NULL) - } - - # проверка на соответствие вариантов схеме --------- - compare_to_dict <- (x %in% choices) - if (!all(compare_to_dict)) { - - text <- paste0("'",x[!compare_to_dict],"'", collapse = ", ") - glue::glue("варианты, не соответствующие схеме: {text}") - } - }) - + iv$add_rule(x_input_id, val_choice_within_a_dict, choices = choices) } - # if in `required` column value is `1` apply standart validation if (!is.na(val_required) && val_required == 1) { iv$add_rule(x_input_id, shinyvalidate::sv_required(message = "Необходимо заполнить.")) @@ -111,4 +63,60 @@ init_val = function(scheme, ns) { } ) iv +} + +# работа с числовыми значениями ------------------ +## проверка является ли значение числом ---------- +val_is_a_number = function(x) { + + # exit if empty + if (is_this_empty_value(x)) { + return(NULL) + } + + # хак для пропуска значений + if (x == "NA") return(NULL) + + # check for numeric + # if (grepl("^[-]?(\\d*\\,\\d+|\\d+\\,\\d*|\\d+)$", x)) NULL else "Значение должно быть числом." + if (grepl("^[+-]?\\d*[\\.|\\,]?\\d+$", x)) NULL else "Значение должно быть числом." + +} + +## находится ли число в заданном диапазоне значений ------- +val_number_within_a_range = function(x, ranges) { + + # exit if empty + if (is_this_empty_value(x)) { + return(NULL) + } + + if (x == "NA") return(NULL) + + # замена разделителя десятичных цифр + x <- stringr::str_replace(x, ",", ".") + + # check for currect value + if (dplyr::between(as.double(x), ranges[1], ranges[2])) { + NULL + } else { + glue::glue("Значение должно быть между {ranges[1]} и {ranges[2]}.") + } +} + +# списки --------------------------------------------------------- +## являются ли выбранные значения допустимы (согласно файлу схемы) +val_choice_within_a_dict = function(x, choices) { + + if (length(x) == 1) { + if (is_this_empty_value(x)) return(NULL) + } + + # проверка на соответствие вариантов схеме --------- + compare_to_dict <- (x %in% choices) + if (!all(compare_to_dict)) { + + text <- paste0("'",x[!compare_to_dict],"'", collapse = ", ") + glue::glue("варианты, не соответствующие схеме: {text}") + } } \ No newline at end of file diff --git a/modules/db.R b/modules/db.R index 98a84b2..68e95dd 100644 --- a/modules/db.R +++ b/modules/db.R @@ -201,9 +201,6 @@ write_df_to_db = function( dplyr::across(tidyselect::all_of({{number_columns}}), ~ gsub("\\.", "," , .x)), dplyr::across(tidyselect::all_of({{other_cols}}), as.character), ) - - df |> - dplyr::glimpse() if (table_name == "main") { del_query <- glue::glue("DELETE FROM main WHERE {main_key_id} = '{main_key_value}'") diff --git a/modules/global_options.R b/modules/global_options.R index 52300d3..eefb0f9 100644 --- a/modules/global_options.R +++ b/modules/global_options.R @@ -23,6 +23,9 @@ check_and_init_scheme = function() { cli::cli_inform(c("*" = "проверка схемы...")) + options(box.path = here::here()) + box::use(configs/enabled_schemes[enabled_schemes]) + files_to_watch <- c( "configs/enabled_schemes.R", "modules/scheme_generator.R", @@ -68,7 +71,6 @@ check_and_init_scheme = function() { saveRDS(exist_hash, hash_file) } - init_scheme = function(scheme_file) { options(box.path = here::here())