Compare commits
2 Commits
f9d84bf245
...
dbfe27bbb8
| Author | SHA1 | Date | |
|---|---|---|---|
| dbfe27bbb8 | |||
| 4225c4d382 |
67
app.R
67
app.R
@@ -40,7 +40,6 @@ if (!rmarkdown::pandoc_available()) warning("Can't find pandoc!")
|
|||||||
|
|
||||||
# SCHEME_MAIN UNPACK ==========================
|
# SCHEME_MAIN UNPACK ==========================
|
||||||
schm <- readRDS("scheme.rds")
|
schm <- readRDS("scheme.rds")
|
||||||
nav_panels_list <- schm$get_main_form_ui
|
|
||||||
|
|
||||||
# two_obj <- purrr::map(
|
# two_obj <- purrr::map(
|
||||||
# c(one = "configs/schemas/schema.xlsx", two = "configs/schemas/schema_example.xlsx"),
|
# c(one = "configs/schemas/schema.xlsx", two = "configs/schemas/schema_example.xlsx"),
|
||||||
@@ -154,27 +153,23 @@ server <- function(input, output, session) {
|
|||||||
nested_key = NULL,
|
nested_key = NULL,
|
||||||
nested_form_id = NULL
|
nested_form_id = NULL
|
||||||
)
|
)
|
||||||
|
main_form_is_empty <- reactiveVal(TRUE)
|
||||||
# VALIDATIONS ============================
|
validator_main <- reactiveVal(NULL)
|
||||||
# create new validator
|
validator_nested <- reactiveVal(NULL)
|
||||||
# TODO: как осуществить инициализацию валидатора после
|
|
||||||
iv_main <- data_validation$init_val(schm$get_schema("main"))
|
|
||||||
iv_main$enable()
|
|
||||||
|
|
||||||
# динамический рендеринг --------------------------
|
# динамический рендеринг --------------------------
|
||||||
output$main_ui_navset <- renderUI({
|
output$main_ui_navset <- renderUI({
|
||||||
|
|
||||||
shiny::validate(
|
if (main_form_is_empty()) {
|
||||||
need(values$main_key, "Для начала работы нужно создать новую запись или загрузить существующую!")
|
validator_main(NULL)
|
||||||
)
|
"Для начала работы нужно создать новую запись или загрузить существующую!"
|
||||||
|
} else {
|
||||||
|
|
||||||
# list of rendered panels
|
# list of rendered panels
|
||||||
navset_card_underline(
|
validator_main(data_validation$init_val(schm$get_schema("main")))
|
||||||
id = "main",
|
validator_main()$enable()
|
||||||
!!!nav_panels_list,
|
schm$get_main_form_ui
|
||||||
header = NULL,
|
}
|
||||||
height = NULL
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
# ==========================================
|
# ==========================================
|
||||||
@@ -377,6 +372,7 @@ server <- function(input, output, session) {
|
|||||||
actionButton("add_new_nested_key_button", "add"),
|
actionButton("add_new_nested_key_button", "add"),
|
||||||
actionButton("nested_form_save_button", "save"),
|
actionButton("nested_form_save_button", "save"),
|
||||||
actionButton("nested_form_dt_button", "dt"),
|
actionButton("nested_form_dt_button", "dt"),
|
||||||
|
uiOutput("validation_nested"),
|
||||||
open = list(mobile = "always-above")
|
open = list(mobile = "always-above")
|
||||||
),
|
),
|
||||||
# if (!is.null(values$nested_key)) {rlang::syms(!!!yay_its_fun)} else bslib::nav_panel("empty")
|
# if (!is.null(values$nested_key)) {rlang::syms(!!!yay_its_fun)} else bslib::nav_panel("empty")
|
||||||
@@ -384,8 +380,8 @@ server <- function(input, output, session) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
# проверка данных для внутренних таблиц
|
# проверка данных для внутренних таблиц
|
||||||
iv_inner <- data_validation$init_val(this_nested_form_scheme, ns)
|
validator_nested(data_validation$init_val(this_nested_form_scheme, ns))
|
||||||
iv_inner$enable()
|
validator_nested()$enable()
|
||||||
|
|
||||||
showModal(modalDialog(
|
showModal(modalDialog(
|
||||||
ui_for_inline_table,
|
ui_for_inline_table,
|
||||||
@@ -450,12 +446,12 @@ server <- function(input, output, session) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
### обновление данных при изменении --------------------
|
### обновление данных при изменении DT --------------------
|
||||||
observeEvent(input$dt_nested_cell_edit, {
|
observeEvent(input$dt_nested_cell_edit, {
|
||||||
values$data <- DT::editData(values$data, input$dt_nested_cell_edit, 'dt_nested', rownames = FALSE)
|
values$data <- DT::editData(values$data, input$dt_nested_cell_edit, 'dt_nested', rownames = FALSE)
|
||||||
})
|
})
|
||||||
|
|
||||||
### кнопка: отображение -----------------------------
|
### кнопка: отображение DT -----------------------------
|
||||||
observeEvent(input$nested_form_dt_button, {
|
observeEvent(input$nested_form_dt_button, {
|
||||||
con <- db$make_db_connection("nested_form_save_button")
|
con <- db$make_db_connection("nested_form_save_button")
|
||||||
on.exit(db$close_db_connection(con, "nested_form_save_button"), add = TRUE)
|
on.exit(db$close_db_connection(con, "nested_form_save_button"), add = TRUE)
|
||||||
@@ -464,7 +460,7 @@ server <- function(input, output, session) {
|
|||||||
show_modal_for_nested_form_dt(con)
|
show_modal_for_nested_form_dt(con)
|
||||||
})
|
})
|
||||||
|
|
||||||
### кнопка: сохранить изменения --------------------
|
### кнопка: сохранить изменения DT --------------------
|
||||||
observeEvent(input$nested_form_dt_save, {
|
observeEvent(input$nested_form_dt_save, {
|
||||||
|
|
||||||
con <- db$make_db_connection("nested_form_dt_save")
|
con <- db$make_db_connection("nested_form_dt_save")
|
||||||
@@ -512,11 +508,22 @@ server <- function(input, output, session) {
|
|||||||
log_action_to_db("saving data", values$main_key, con)
|
log_action_to_db("saving data", values$main_key, con)
|
||||||
|
|
||||||
showNotification(
|
showNotification(
|
||||||
"Данные успешно сохраннены",
|
"Данные успешно сохранены",
|
||||||
type = "message"
|
type = "message"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
## nested validation ------------------------------------
|
||||||
|
output$validation_nested <- renderText({
|
||||||
|
req(validator_nested())
|
||||||
|
|
||||||
|
if (validator_nested()$is_valid()) {
|
||||||
|
"✅ Данные внесены корректно"
|
||||||
|
} else {
|
||||||
|
"⚠️ Данные внесены не полностью или некорректно"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
## обновление данных при переключении ключей ------------
|
## обновление данных при переключении ключей ------------
|
||||||
observeEvent(input$nested_key_selector, {
|
observeEvent(input$nested_key_selector, {
|
||||||
req(input$nested_key_selector)
|
req(input$nested_key_selector)
|
||||||
@@ -620,7 +627,13 @@ server <- function(input, output, session) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
output$status_message2 <- renderText({
|
output$status_message2 <- renderText({
|
||||||
iv_main$is_valid()
|
req(validator_main())
|
||||||
|
|
||||||
|
if (validator_main()$is_valid()) {
|
||||||
|
"✅ Данные внесены корректно"
|
||||||
|
} else {
|
||||||
|
"⚠️ Данные внесены не полностью или некорректно"
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
# =========================================
|
# =========================================
|
||||||
@@ -673,6 +686,7 @@ server <- function(input, output, session) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
values$main_key <- new_main_key
|
values$main_key <- new_main_key
|
||||||
|
main_form_is_empty(FALSE)
|
||||||
log_action_to_db("creating new key", values$main_key, con)
|
log_action_to_db("creating new key", values$main_key, con)
|
||||||
utils$clean_forms("main", schm)
|
utils$clean_forms("main", schm)
|
||||||
|
|
||||||
@@ -697,8 +711,9 @@ server <- function(input, output, session) {
|
|||||||
observeEvent(input$clean_all_action, {
|
observeEvent(input$clean_all_action, {
|
||||||
|
|
||||||
# rewrite all inputs with empty data
|
# rewrite all inputs with empty data
|
||||||
utils$clean_forms("main", schm)
|
|
||||||
values$main_key <- NULL
|
values$main_key <- NULL
|
||||||
|
utils$clean_forms("main", schm)
|
||||||
|
main_form_is_empty(TRUE)
|
||||||
|
|
||||||
removeModal()
|
removeModal()
|
||||||
showNotification("Данные очищены!", type = "warning")
|
showNotification("Данные очищены!", type = "warning")
|
||||||
@@ -788,6 +803,8 @@ server <- function(input, output, session) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
values$main_key <- input$load_data_key_selector
|
values$main_key <- input$load_data_key_selector
|
||||||
|
main_form_is_empty(FALSE)
|
||||||
|
|
||||||
log_action_to_db("loading data", values$main_key, con = con)
|
log_action_to_db("loading data", values$main_key, con = con)
|
||||||
removeModal()
|
removeModal()
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#' not 'empty').
|
#' not 'empty').
|
||||||
#'
|
#'
|
||||||
#' Needed for proper data validation.
|
#' Needed for proper data validation.
|
||||||
check_for_empty_data <- function(value_to_check) {
|
check_for_empty_data = function(value_to_check) {
|
||||||
|
|
||||||
# for any 0-length
|
# for any 0-length
|
||||||
if (length(value_to_check) == 0) return(TRUE)
|
if (length(value_to_check) == 0) return(TRUE)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
#' @export
|
#' @export
|
||||||
init_val <- function(scheme, ns) {
|
init_val = function(scheme, ns) {
|
||||||
|
|
||||||
options(box.path = here::here())
|
options(box.path = here::here())
|
||||||
box::use(modules/data_manipulations[check_for_empty_data])
|
box::use(modules/data_manipulations[check_for_empty_data])
|
||||||
|
|||||||
22
modules/db.R
22
modules/db.R
@@ -2,7 +2,7 @@
|
|||||||
#' @export
|
#' @export
|
||||||
#' @description Function to open connection to db, disigned to easy dubugging.
|
#' @description Function to open connection to db, disigned to easy dubugging.
|
||||||
#' @param where text mark to distingiush calss
|
#' @param where text mark to distingiush calss
|
||||||
make_db_connection <- function(where = "") {
|
make_db_connection = function(where = "") {
|
||||||
if (getOption("APP.DEBUG", FALSE)) message("=== DB CONNECT ", where)
|
if (getOption("APP.DEBUG", FALSE)) message("=== DB CONNECT ", where)
|
||||||
DBI::dbConnect(RSQLite::SQLite(), getOption("APP.FILE_DB", FALSE))
|
DBI::dbConnect(RSQLite::SQLite(), getOption("APP.FILE_DB", FALSE))
|
||||||
}
|
}
|
||||||
@@ -11,7 +11,7 @@ make_db_connection <- function(where = "") {
|
|||||||
#' @description
|
#' @description
|
||||||
#' Function to close connection to db, disigned to easy dubugging and
|
#' Function to close connection to db, disigned to easy dubugging and
|
||||||
#' hide warnings.
|
#' hide warnings.
|
||||||
close_db_connection <- function(con, where = "") {
|
close_db_connection = function(con, where = "") {
|
||||||
tryCatch(
|
tryCatch(
|
||||||
expr = DBI::dbDisconnect(con),
|
expr = DBI::dbDisconnect(con),
|
||||||
error = function(e) print(e),
|
error = function(e) print(e),
|
||||||
@@ -23,7 +23,7 @@ close_db_connection <- function(con, where = "") {
|
|||||||
#' @export
|
#' @export
|
||||||
#' @description
|
#' @description
|
||||||
#' Проверить если таблица есть в базе данных и инициировать ее, если от
|
#' Проверить если таблица есть в базе данных и инициировать ее, если от
|
||||||
check_if_table_is_exist_and_init_if_not <- function(
|
check_if_table_is_exist_and_init_if_not = function(
|
||||||
schm,
|
schm,
|
||||||
con = rlang::env_get(rlang::caller_env(), nm = "con")
|
con = rlang::env_get(rlang::caller_env(), nm = "con")
|
||||||
) {
|
) {
|
||||||
@@ -79,7 +79,7 @@ check_if_table_is_exist_and_init_if_not <- function(
|
|||||||
|
|
||||||
#' @description
|
#' @description
|
||||||
#' Возращает пустое значение для каждого типа формы
|
#' Возращает пустое значение для каждого типа формы
|
||||||
get_dummy_data <- function(type) {
|
get_dummy_data = function(type) {
|
||||||
|
|
||||||
if (type %in% c("text", "select_one", "select_multiple")) return("dummy")
|
if (type %in% c("text", "select_one", "select_multiple")) return("dummy")
|
||||||
if (type %in% c("radio", "checkbox")) return("dummy")
|
if (type %in% c("radio", "checkbox")) return("dummy")
|
||||||
@@ -92,7 +92,7 @@ get_dummy_data <- function(type) {
|
|||||||
#' @description
|
#' @description
|
||||||
#' Генерация пустого датасета с пустыми значениями соответствующие
|
#' Генерация пустого датасета с пустыми значениями соответствующие
|
||||||
#' типу данных
|
#' типу данных
|
||||||
get_dummy_df <- function(forms_id_type_list) {
|
get_dummy_df = function(forms_id_type_list) {
|
||||||
|
|
||||||
options(box.path = here::here())
|
options(box.path = here::here())
|
||||||
box::use(modules/utils)
|
box::use(modules/utils)
|
||||||
@@ -108,7 +108,7 @@ get_dummy_df <- function(forms_id_type_list) {
|
|||||||
#' @description
|
#' @description
|
||||||
#' Сравнение полей в существующей в базе данных таблице и попытка
|
#' Сравнение полей в существующей в базе данных таблице и попытка
|
||||||
#' коррекции таблицы
|
#' коррекции таблицы
|
||||||
compare_existing_table_with_schema <- function(
|
compare_existing_table_with_schema = function(
|
||||||
table_name,
|
table_name,
|
||||||
schm,
|
schm,
|
||||||
con = rlang::env_get(rlang::caller_env(), nm = "con")
|
con = rlang::env_get(rlang::caller_env(), nm = "con")
|
||||||
@@ -176,7 +176,7 @@ compare_existing_table_with_schema <- function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#' @export
|
#' @export
|
||||||
write_df_to_db <- function(
|
write_df_to_db = function(
|
||||||
df,
|
df,
|
||||||
table_name,
|
table_name,
|
||||||
schm,
|
schm,
|
||||||
@@ -225,7 +225,7 @@ write_df_to_db <- function(
|
|||||||
|
|
||||||
#' @export
|
#' @export
|
||||||
#' reading tables from db by name and id ========
|
#' reading tables from db by name and id ========
|
||||||
read_df_from_db_by_id <- function(
|
read_df_from_db_by_id = function(
|
||||||
table_name,
|
table_name,
|
||||||
schm,
|
schm,
|
||||||
main_key_value,
|
main_key_value,
|
||||||
@@ -264,7 +264,7 @@ read_df_from_db_by_id <- function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#' @export
|
#' @export
|
||||||
get_keys_from_table <- function(table_name, schm, con) {
|
get_keys_from_table = function(table_name, schm, con) {
|
||||||
|
|
||||||
main_key_id <- schm$get_main_key_id
|
main_key_id <- schm$get_main_key_id
|
||||||
DBI::dbGetQuery(con, glue::glue("SELECT DISTINCT {main_key_id} FROM {table_name}")) |>
|
DBI::dbGetQuery(con, glue::glue("SELECT DISTINCT {main_key_id} FROM {table_name}")) |>
|
||||||
@@ -273,7 +273,7 @@ get_keys_from_table <- function(table_name, schm, con) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#' @export
|
#' @export
|
||||||
get_nested_keys_from_table <- function(table_name, schm, main_key_value, con) {
|
get_nested_keys_from_table = function(table_name, schm, main_key_value, con) {
|
||||||
|
|
||||||
main_key_id <- schm$get_main_key_id
|
main_key_id <- schm$get_main_key_id
|
||||||
key_id <- schm$get_key_id(table_name)
|
key_id <- schm$get_key_id(table_name)
|
||||||
@@ -286,7 +286,7 @@ get_nested_keys_from_table <- function(table_name, schm, main_key_value, con) {
|
|||||||
|
|
||||||
### HELPERS ---------
|
### HELPERS ---------
|
||||||
#' @export
|
#' @export
|
||||||
excel_to_db_dates_converter <- function(date) {
|
excel_to_db_dates_converter = function(date) {
|
||||||
|
|
||||||
if (is.na(date)) return(NA)
|
if (is.na(date)) return(NA)
|
||||||
# cli::cli_inform("date: {date} | nchar: {nchar(date)} | typeof: {typeof(date)}")
|
# cli::cli_inform("date: {date} | nchar: {nchar(date)} | typeof: {typeof(date)}")
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#' @export
|
#' @export
|
||||||
#' @description костыли для упрощения работы себе
|
#' @description костыли для упрощения работы себе
|
||||||
set_global_options <- function(
|
set_global_options = function(
|
||||||
SYMBOL_DELIM = "; ",
|
SYMBOL_DELIM = "; ",
|
||||||
APP.DEBUG = FALSE,
|
APP.DEBUG = FALSE,
|
||||||
APP.FILE_DB = fs::path("data.sqlite"),
|
APP.FILE_DB = fs::path("data.sqlite"),
|
||||||
@@ -19,7 +19,7 @@ set_global_options <- function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#' @export
|
#' @export
|
||||||
check_and_init_scheme <- function() {
|
check_and_init_scheme = function() {
|
||||||
|
|
||||||
cli::cli_inform(c("*" = "проверка схемы..."))
|
cli::cli_inform(c("*" = "проверка схемы..."))
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ check_and_init_scheme <- function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
init_scheme <- function(scheme_file) {
|
init_scheme = function(scheme_file) {
|
||||||
|
|
||||||
options(box.path = here::here())
|
options(box.path = here::here())
|
||||||
box::use(
|
box::use(
|
||||||
|
|||||||
@@ -36,7 +36,12 @@ scheme_R6 <- R6::R6Class(
|
|||||||
private$main_key_id <- self$get_key_id("main")
|
private$main_key_id <- self$get_key_id("main")
|
||||||
|
|
||||||
box::use(modules/utils)
|
box::use(modules/utils)
|
||||||
private$testest <- utils$make_list_of_pages(private$schemes_list[["main"]], private$main_key_id)
|
private$testest <- bslib::navset_card_underline(
|
||||||
|
id = "main",
|
||||||
|
!!!utils$make_list_of_pages(private$schemes_list[["main"]], private$main_key_id),
|
||||||
|
header = NULL,
|
||||||
|
height = NULL
|
||||||
|
)
|
||||||
},
|
},
|
||||||
|
|
||||||
get_all_ids = function(table_name) {
|
get_all_ids = function(table_name) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#' @export
|
#' @export
|
||||||
make_list_of_pages <- function(main_schema, main_key_id) {
|
make_list_of_pages = function(main_schema, main_key_id) {
|
||||||
cli::cli_alert_success("AAAA")
|
cli::cli_alert_success("AAAA")
|
||||||
purrr::map(
|
purrr::map(
|
||||||
.x = unique(main_schema$part),
|
.x = unique(main_schema$part),
|
||||||
@@ -22,7 +22,7 @@ make_list_of_pages <- function(main_schema, main_key_id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#' @export
|
#' @export
|
||||||
make_panels <- function(scheme) {
|
make_panels = function(scheme) {
|
||||||
|
|
||||||
cards <- purrr::map(
|
cards <- purrr::map(
|
||||||
.x = unique(scheme$subgroup),
|
.x = unique(scheme$subgroup),
|
||||||
@@ -61,7 +61,7 @@ make_panels <- function(scheme) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#' @export
|
#' @export
|
||||||
render_forms <- function(
|
render_forms = function(
|
||||||
form_id,
|
form_id,
|
||||||
form_label,
|
form_label,
|
||||||
form_type,
|
form_type,
|
||||||
@@ -242,7 +242,7 @@ render_forms <- function(
|
|||||||
#' @export
|
#' @export
|
||||||
#' @description
|
#' @description
|
||||||
#' Функция возращает пустое значение для каждого типа формы
|
#' Функция возращает пустое значение для каждого типа формы
|
||||||
get_empty_data <- function(type) {
|
get_empty_data = function(type) {
|
||||||
if (type %in% c("text", "select_one", "select_multiple")) return(as.character(NA))
|
if (type %in% c("text", "select_one", "select_multiple")) return(as.character(NA))
|
||||||
if (type %in% c("radio", "checkbox")) return(as.character(NA))
|
if (type %in% c("radio", "checkbox")) return(as.character(NA))
|
||||||
if (type %in% c("date")) return(as.Date(NA))
|
if (type %in% c("date")) return(as.Date(NA))
|
||||||
@@ -255,7 +255,7 @@ get_empty_data <- function(type) {
|
|||||||
#' @param type - type of form;
|
#' @param type - type of form;
|
||||||
#' @param value - value to update;
|
#' @param value - value to update;
|
||||||
#' @param local_delimeter - delimeter to split file
|
#' @param local_delimeter - delimeter to split file
|
||||||
update_forms_with_data <- function(
|
update_forms_with_data = function(
|
||||||
form_id,
|
form_id,
|
||||||
form_type,
|
form_type,
|
||||||
value,
|
value,
|
||||||
@@ -341,7 +341,7 @@ update_forms_with_data <- function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#' @export
|
#' @export
|
||||||
clean_forms <- function(
|
clean_forms = function(
|
||||||
table_name,
|
table_name,
|
||||||
schm,
|
schm,
|
||||||
ns
|
ns
|
||||||
@@ -366,5 +366,4 @@ clean_forms <- function(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user