Skip to content

Commit

Permalink
Merge pull request IDEMSInternational#9217 from N-thony/bug_fix_appen…
Browse files Browse the repository at this point in the history
…d_varmetadata

Bug fixes in the append to variable metadata function
  • Loading branch information
N-thony authored Oct 31, 2024
2 parents 9f83040 + 03ac977 commit 62fa9fe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
35 changes: 18 additions & 17 deletions instat/static/InstatObject/R/Backend_Components/summary_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@ DataSheet$set("public", "merge_data", function(new_data, by = NULL, type = "left
curr_data <- self$get_data_frame(use_current_filter = FALSE)
by_col_attributes <- list()

if(!is.null(by)) {
for(i in seq_along(by)) {
# Collect column attributes
by_col_attributes[[by[[i]]]] <- get_column_attributes(curr_data[[by[[i]]]])

# Check and align the data types for each "by" column
if (class(curr_data[[by[[i]]]]) != class(new_data[[by[[i]]]])) {
warning(paste0("Type is different for ", by[[i]], " in the two data frames. Setting as numeric in both data frames."))

# Convert factors to numeric if necessary
if (class(curr_data[[by[[i]]]]) == "factor") {
curr_data[[by[[i]]]] <- as.numeric(as.character(curr_data[[by[[i]]]]))
} else if (class(new_data[[by[[i]]]]) == "factor") {
new_data[[by[[i]]]] <- as.numeric(as.character(new_data[[by[[i]]]]))
} else {
stop(paste0("Type is different for ", by[[i]], " in the two data frames and cannot be coerced."))
}
if (!is.null(by)) {
for (i in seq_along(by)) {
# Collect column attributes
by_col_attributes[[by[[i]]]] <- get_column_attributes(curr_data[[by[[i]]]])

# Check and align the data types for each "by" column
if (!inherits(curr_data[[by[[i]]]], class(new_data[[by[[i]]]]))) {
warning(paste0("Type is different for ", by[[i]], " in the two data frames. Setting as numeric in both data frames."))

# Convert factors to numeric if necessary
if (inherits(curr_data[[by[[i]]]], "factor")) {
curr_data[[by[[i]]]] <- as.numeric(as.character(curr_data[[by[[i]]]]))
} else if (inherits(new_data[[by[[i]]]], "factor")) {
new_data[[by[[i]]]] <- as.numeric(as.character(new_data[[by[[i]]]]))
} else {
stop(paste0("Type is different for ", by[[i]], " in the two data frames and cannot be coerced."))
}
}
}
}


# Perform the appropriate join based on the "type" argument
if (type == "left") {
Expand Down
4 changes: 2 additions & 2 deletions instat/static/InstatObject/R/data_object_R6.R
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ DataSheet$set("public", "append_to_variables_metadata", function(col_names, prop
for (curr_col in col_names) {
#see comments in PR #7247 to understand why ' property == labels_label && new_val == "" ' check was added
#see comments in issue #7337 to understand why the !is.null(new_val) check was added.
if (((property == labels_label && new_val == "") || (property == colour_label && new_val == -1)) && !is.null(new_val)) {
if (((property == labels_label && any(new_val == "")) || (property == colour_label && new_val == -1)) && !is.null(new_val)) {
#reset the column labels or colour property
attr(private$data[[curr_col]], property) <- NULL
} else {
Expand All @@ -1308,7 +1308,7 @@ DataSheet$set("public", "append_to_variables_metadata", function(col_names, prop
for (col_name in self$get_column_names()) {
#see comments in PR #7247 to understand why ' property == labels_label && new_val == "" ' check was added
#see comments in issue #7337 to understand why the !is.null(new_val) check was added.
if (((property == labels_label && new_val == "") || (property == colour_label && new_val == -1)) && !is.null(new_val)) {
if (((property == labels_label && any(new_val == "")) || (property == colour_label && new_val == -1)) && !is.null(new_val)) {
#reset the column labels or colour property
attr(private$data[[col_name]], property) <- NULL
} else {
Expand Down

0 comments on commit 62fa9fe

Please sign in to comment.