Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensured that columns in the Data Grid can be deleted even when there are no key columns #8422

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions instat/static/InstatObject/R/data_object_R6.R
Original file line number Diff line number Diff line change
Expand Up @@ -771,11 +771,14 @@ DataSheet$set("public", "rename_column_in_data", function(curr_col_name = "", ne
warning("Multiple columns have name: '", curr_col_name, "'. All such columns will be renamed.")
}
# remove key
if (self$get_variables_metadata() %>% dplyr::filter(Name == curr_col_name) %>% dplyr::pull(Is_Key)){
active_keys <- self$get_keys()
keys_to_delete <- which(grepl(curr_col_name, active_keys))
keys_to_delete <- purrr::map_chr(.x = keys_to_delete, .f = ~names(active_keys[.x]))
purrr::map(.x = keys_to_delete, .f = ~self$remove_key(key_name = names(active_keys[.x])))
get_key <- self$get_variables_metadata() %>% dplyr::filter(Name == col_name)
if (!is.null(get_key$Is_Key)){
if (!is.na(get_key$Is_Key) && get_key$Is_Key){
active_keys <- self$get_keys()
keys_to_delete <- which(grepl(curr_col_name, active_keys))
keys_to_delete <- purrr::map_chr(.x = keys_to_delete, .f = ~names(active_keys[.x]))
purrr::map(.x = keys_to_delete, .f = ~self$remove_key(key_name = names(active_keys[.x])))
}
}
# Need to use private$data here because changing names of data field
names(private$data)[names(curr_data) == curr_col_name] <- new_col_name
Expand Down Expand Up @@ -816,11 +819,11 @@ DataSheet$set("public", "rename_column_in_data", function(curr_col_name = "", ne
} else if (type == "rename_with") {
if (missing(.fn)) stop(.fn, "is missing with no default.")
curr_col_names <- names(curr_data)
private$data <- curr_data |>

private$data <- curr_data |>
dplyr::rename_with(
.fn = .fn,
.cols = {{ .cols }}, ...
.fn = .fn,
.cols = {{ .cols }}, ...
)
new_col_names <- names(private$data)
if (!all(new_col_names %in% curr_col_names)) {
Expand All @@ -834,6 +837,8 @@ DataSheet$set("public", "rename_column_in_data", function(curr_col_name = "", ne
}
})



DataSheet$set("public", "remove_columns_in_data", function(cols=c(), allow_delete_all = FALSE) {
if(length(cols) == self$get_column_count()) {
if(allow_delete_all) {
Expand All @@ -843,24 +848,27 @@ DataSheet$set("public", "remove_columns_in_data", function(cols=c(), allow_delet
}
}
for(col_name in cols) {
# Column name must be character
if(!is.character(col_name)) {
stop("Column name must be of type: character")
} else if (!(col_name %in% self$get_column_names())) {
stop(paste0("Column :'", col_name, " was not found in the data."))
} else {
if (self$get_variables_metadata() %>% dplyr::filter(Name == col_name) %>% dplyr::pull(Is_Key)){
active_keys <- self$get_keys()
# Column name must be character
if(!is.character(col_name)) {
stop("Column name must be of type: character")
} else if (!(col_name %in% self$get_column_names())) {
stop(paste0("Column :'", col_name, " was not found in the data."))
} else {
get_key <- self$get_variables_metadata() %>% dplyr::filter(Name == col_name)
if (!is.null(get_key$Is_Key)){
if (!is.na(get_key$Is_Key) && get_key$Is_Key){
active_keys <- self$get_keys()
keys_to_delete <- which(grepl(col_name, active_keys))
keys_to_delete <- purrr::map_chr(.x = keys_to_delete, .f = ~names(active_keys[.x]))
purrr::map(.x = keys_to_delete, .f = ~self$remove_key(key_name = names(active_keys[.x])))
purrr::map(.x = keys_to_delete, .f = ~self$remove_key(key_name = names(active_keys[.x])))
}
private$data[[col_name]] <- NULL
}
self$append_to_changes(list(Removed_col, cols))
self$data_changed <- TRUE
self$variables_metadata_changed <- TRUE
private$data[[col_name]] <- NULL
}
self$append_to_changes(list(Removed_col, cols))
self$data_changed <- TRUE
self$variables_metadata_changed <- TRUE
}
}
)

Expand Down