feat: предсказуемый экспорт числовых значений
This commit is contained in:
@@ -45,4 +45,10 @@
|
|||||||
|
|
||||||
```
|
```
|
||||||
AUTH_DB_KEY = "this_is_your_password"
|
AUTH_DB_KEY = "this_is_your_password"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
# trade-ofs
|
||||||
|
|
||||||
|
## экспорт данных
|
||||||
|
|
||||||
|
Числовые значения выгружаются в xlsx в виде текстовых значений, чтобы сохранить указаные "NA" в базе. Потенциально возможно добавление опции при экспорте для корректного экспотра числовых значений.
|
||||||
21
app.R
21
app.R
@@ -812,7 +812,7 @@ server <- function(input, output, session) {
|
|||||||
# set date params
|
# set date params
|
||||||
options("openxlsx2.dateFormat" = "dd.mm.yyyy")
|
options("openxlsx2.dateFormat" = "dd.mm.yyyy")
|
||||||
|
|
||||||
cli::cli_alert_success("DATA EXPORTED")
|
cli::cli_alert_success("База успешно экспортирована")
|
||||||
showNotification("База успешно экспортирована", type = "message")
|
showNotification("База успешно экспортирована", type = "message")
|
||||||
|
|
||||||
log_action_to_db("export db", con = con)
|
log_action_to_db("export db", con = con)
|
||||||
@@ -996,11 +996,26 @@ server <- function(input, output, session) {
|
|||||||
filter(!form_type %in% c("description", "nested_forms"))
|
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)
|
||||||
|
|
||||||
|
# функция для преобразование числовых значений и сохранения "NA"
|
||||||
|
num_converter <- function(old_col) {
|
||||||
|
vec_with_na <- which(old_col == "NA")
|
||||||
|
|
||||||
|
# текстовые и числовые значения в текст: '24.0', '24,5' > '24', '24,5' (также обрезеаются десятичные значения где не нужно)
|
||||||
|
new_col <- suppressWarnings(as.character(as.double(gsub(",", "\\.", old_col))))
|
||||||
|
|
||||||
|
# значения где были явно указаны 'NA' остаются с текстом 'NA'
|
||||||
|
new_col[which(old_col == "NA")] <- "NA"
|
||||||
|
|
||||||
|
gsub("\\.", ",", new_col)
|
||||||
|
}
|
||||||
|
|
||||||
df <- df |>
|
df <- df |>
|
||||||
dplyr::mutate(
|
dplyr::mutate(
|
||||||
# даты - к единому формату
|
# даты - к единому формату
|
||||||
dplyr::across(tidyselect::all_of({{date_columns}}), \(x) purrr::map_chr(x, db$excel_to_db_dates_converter)),
|
dplyr::across(tidyselect::all_of({{date_columns}}), \(x) purrr::map_chr(x, db$excel_to_db_dates_converter)),
|
||||||
|
dplyr::across(tidyselect::all_of({{number_columns}}), num_converter),
|
||||||
) |>
|
) |>
|
||||||
select(all_of(unique(c(main_key_id, scheme$form_id))))
|
select(all_of(unique(c(main_key_id, scheme$form_id))))
|
||||||
|
|
||||||
@@ -1044,8 +1059,6 @@ server <- function(input, output, session) {
|
|||||||
# FUNCTIONS ==============================
|
# FUNCTIONS ==============================
|
||||||
## reading tables from db all ========
|
## reading tables from db all ========
|
||||||
read_df_from_db_all <- function(table_name, con) {
|
read_df_from_db_all <- function(table_name, con) {
|
||||||
# DBI::dbConnect(RSQLite::SQLite(), dbfile)
|
|
||||||
# on.exit(DBI::dbDisconnect(con), add = TRUE)
|
|
||||||
|
|
||||||
# check if this table exist
|
# check if this table exist
|
||||||
if (table_name %in% dbListTables(con)) {
|
if (table_name %in% dbListTables(con)) {
|
||||||
@@ -1061,8 +1074,6 @@ server <- function(input, output, session) {
|
|||||||
|
|
||||||
## LOGGING ACTIONS
|
## LOGGING ACTIONS
|
||||||
log_action_to_db <- function(action, pat_id = as.character(NA), con) {
|
log_action_to_db <- function(action, pat_id = as.character(NA), con) {
|
||||||
# DBI::dbConnect(RSQLite::SQLite(), dbfile)
|
|
||||||
# on.exit(DBI::dbDisconnect(con), add = TRUE)
|
|
||||||
|
|
||||||
action_row <- tibble(
|
action_row <- tibble(
|
||||||
user = ifelse(AUTH_ENABLED, res_auth$user, "anonymous"),
|
user = ifelse(AUTH_ENABLED, res_auth$user, "anonymous"),
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ init_val <- function(scheme, ns) {
|
|||||||
if (!all(compare_to_dict)) {
|
if (!all(compare_to_dict)) {
|
||||||
|
|
||||||
text <- paste0("'",x[!compare_to_dict],"'", collapse = ", ")
|
text <- paste0("'",x[!compare_to_dict],"'", collapse = ", ")
|
||||||
glue::glue("Данные варианты, не соответствуют схеме: {text}")
|
glue::glue("варианты, не соответствующие схеме: {text}")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -141,7 +141,6 @@ compare_existing_table_with_schema <- function(
|
|||||||
length(form_base_difference) == 0 &&
|
length(form_base_difference) == 0 &&
|
||||||
length(base_form_difference) == 0) {
|
length(base_form_difference) == 0) {
|
||||||
cli::cli_warn("changes in scheme file detected: assuming order changed only")
|
cli::cli_warn("changes in scheme file detected: assuming order changed only")
|
||||||
print(all_ids_from_schema)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (length(all_ids_from_schema) == length(colnames(df_to_rewrite)) &&
|
if (length(all_ids_from_schema) == length(colnames(df_to_rewrite)) &&
|
||||||
|
|||||||
Reference in New Issue
Block a user