feat: возможность полного обновления базы данных из эксель-таблицы
This commit is contained in:
36
app.R
36
app.R
@@ -55,7 +55,7 @@ ui <- page_sidebar(
|
|||||||
actionButton("clean_data_button", "Главная страница", icon("house", lib = "font-awesome")),
|
actionButton("clean_data_button", "Главная страница", icon("house", lib = "font-awesome")),
|
||||||
actionButton("load_data_button", "Загрузить данные", icon("pencil", lib = "font-awesome")),
|
actionButton("load_data_button", "Загрузить данные", icon("pencil", lib = "font-awesome")),
|
||||||
downloadButton("downloadDocx", "get .docx (test only)"),
|
downloadButton("downloadDocx", "get .docx (test only)"),
|
||||||
textOutput("status_message"),
|
uiOutput("status_message"),
|
||||||
textOutput("status_message2"),
|
textOutput("status_message2"),
|
||||||
uiOutput("display_log"),
|
uiOutput("display_log"),
|
||||||
position = "left",
|
position = "left",
|
||||||
@@ -650,12 +650,16 @@ server <- function(input, output, session) {
|
|||||||
|
|
||||||
# STATUSES ===============================
|
# STATUSES ===============================
|
||||||
# вывести отображение что что-то не так
|
# вывести отображение что что-то не так
|
||||||
output$status_message <- renderText({
|
output$status_message <- renderUI({
|
||||||
scheme()
|
|
||||||
shiny::validate(
|
shiny::validate(
|
||||||
need(values$main_key, "⚠️ Необходимо указать id пациента!")
|
need(values$main_key, "⚠️ Необходимо указать id пациента!")
|
||||||
)
|
)
|
||||||
paste0("ID: ", values$main_key)
|
span(
|
||||||
|
strong("Таблица: "), names(enabled_schemas)[enabled_schemas == scheme()],
|
||||||
|
br(),
|
||||||
|
strong("ID: "), values$main_key
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
output$status_message2 <- renderText({
|
output$status_message2 <- renderText({
|
||||||
@@ -806,8 +810,8 @@ server <- function(input, output, session) {
|
|||||||
ui_load_menu,
|
ui_load_menu,
|
||||||
title = "Загрузить имеющиеся данные",
|
title = "Загрузить имеющиеся данные",
|
||||||
footer = tagList(
|
footer = tagList(
|
||||||
actionButton("close_modal_button", "Отмена", class = "btn btn-danger"),
|
|
||||||
actionButton("load_data", "Загрузить данные"),
|
actionButton("load_data", "Загрузить данные"),
|
||||||
|
actionButton("close_modal_button", "Отмена", class = "btn-danger"),
|
||||||
),
|
),
|
||||||
easyClose = TRUE
|
easyClose = TRUE
|
||||||
)
|
)
|
||||||
@@ -1005,8 +1009,8 @@ server <- function(input, output, session) {
|
|||||||
),
|
),
|
||||||
checkboxInput("upload_data_from_xlsx_owerwrite_all_data", "перезаписать все данные", width = 450),
|
checkboxInput("upload_data_from_xlsx_owerwrite_all_data", "перезаписать все данные", width = 450),
|
||||||
footer = tagList(
|
footer = tagList(
|
||||||
modalButton("Отмена"),
|
actionButton("button_upload_data_from_xlsx_confirm", "Добавить"),
|
||||||
actionButton("button_upload_data_from_xlsx_confirm", "Добавить")
|
actionButton("close_modal_button", "Отмена", class = "btn-danger")
|
||||||
),
|
),
|
||||||
easyClose = FALSE
|
easyClose = FALSE
|
||||||
))
|
))
|
||||||
@@ -1034,19 +1038,17 @@ server <- function(input, output, session) {
|
|||||||
for (table_name in mhcs()$all_tables_names) {
|
for (table_name in mhcs()$all_tables_names) {
|
||||||
|
|
||||||
df <- openxlsx2::read_xlsx(wb, table_name)
|
df <- openxlsx2::read_xlsx(wb, table_name)
|
||||||
scheme <- mhcs()$get_schema(table_name) |>
|
scheme <- mhcs()$get_schema_with_values_forms(table_name)
|
||||||
filter(!form_type %in% c("description", "nested_forms"))
|
|
||||||
|
|
||||||
# столбцы в таблицы и схема
|
# столбцы в таблицы и схема
|
||||||
df_to_schema_compare <- setdiff(colnames(df), unique(scheme$form_id))
|
df_to_schema_compare <- setdiff(colnames(df), unique(scheme$form_id))
|
||||||
schema_to_df_compare <- setdiff(unique(scheme$form_id), colnames(df))
|
schema_to_df_compare <- setdiff(unique(scheme$form_id), colnames(df))
|
||||||
|
|
||||||
if (length(schema_to_df_compare) > 0 ) {
|
if (length(df_to_schema_compare) > 0 ) {
|
||||||
cli::cli_warn(c("в схеме для '{table_name}' нет следующих столбцов:", paste("- ", df_to_schema_compare)))
|
cli::cli_warn(c("в схеме для '{table_name}' нет следующих столбцов:", paste("- ", df_to_schema_compare)))
|
||||||
}
|
}
|
||||||
|
|
||||||
# схема и столбцы в таблице
|
# схема и столбцы в таблице
|
||||||
schema_to_df_compare <- setdiff(unique(scheme$form_id), colnames(df))
|
|
||||||
if (length(schema_to_df_compare) > 0 ) {
|
if (length(schema_to_df_compare) > 0 ) {
|
||||||
|
|
||||||
message <- glue::glue("столбцы в таблице '{table_name}' не соответсвуют схеме")
|
message <- glue::glue("столбцы в таблице '{table_name}' не соответсвуют схеме")
|
||||||
@@ -1061,8 +1063,7 @@ server <- function(input, output, session) {
|
|||||||
for (table_name in mhcs()$all_tables_names) {
|
for (table_name in mhcs()$all_tables_names) {
|
||||||
|
|
||||||
df <- openxlsx2::read_xlsx(wb, table_name)
|
df <- openxlsx2::read_xlsx(wb, table_name)
|
||||||
scheme <- mhcs()$get_schema(table_name) |>
|
scheme <- mhcs()$get_schema_with_values_forms(table_name)
|
||||||
filter(!form_type %in% c("description", "nested_forms"))
|
|
||||||
|
|
||||||
date_columns <- subset(scheme, form_type == "date", form_id, drop = TRUE)
|
date_columns <- subset(scheme, form_type == "date", form_id, drop = TRUE)
|
||||||
number_columns <- subset(scheme, form_type == "number", form_id, drop = TRUE)
|
number_columns <- subset(scheme, form_type == "number", form_id, drop = TRUE)
|
||||||
@@ -1091,8 +1092,13 @@ server <- function(input, output, session) {
|
|||||||
df_original <- DBI::dbReadTable(con, table_name) |>
|
df_original <- DBI::dbReadTable(con, table_name) |>
|
||||||
as_tibble()
|
as_tibble()
|
||||||
|
|
||||||
if (input$upload_data_from_xlsx_owerwrite_all_data == TRUE) cli::cli_abort("not implemented yet")
|
if (input$upload_data_from_xlsx_owerwrite_all_data == TRUE) {
|
||||||
|
|
||||||
|
DBI::dbRemoveTable(con, table_name)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
# удаление данных в базе данных по ключам
|
||||||
walk(
|
walk(
|
||||||
.x = unique(df[[main_key_id]]),
|
.x = unique(df[[main_key_id]]),
|
||||||
.f = \(main_key) {
|
.f = \(main_key) {
|
||||||
@@ -1103,6 +1109,8 @@ server <- function(input, output, session) {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
DBI::dbWriteTable(
|
DBI::dbWriteTable(
|
||||||
con,
|
con,
|
||||||
name = table_name,
|
name = table_name,
|
||||||
|
|||||||
Reference in New Issue
Block a user