diff --git a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R index 1753cead..3374eb50 100644 --- a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R +++ b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R @@ -787,46 +787,40 @@ filter_and_print_non_finite <- function(df, vars_to_check, print_vars) { df %>% filter(if_all(all_of(vars_to_check), is.finite)) } -filter_data_for_plots <- function(df, variables, missing = TRUE, limits_map = NULL) { - - # Print missing data and out-of-range data separately +filter_data_for_plots <- function(df, variables, missing = FALSE, limits_map = NULL) { + + # Loop through each variable to filter and print missing/out-of-range data for (variable in variables) { y_var_sym <- sym(variable) - + + # Filter missing data if (missing) { missing_data <- df %>% filter(is.na(!!y_var_sym)) if (nrow(missing_data) > 0) { message("Missing data for variable ", variable, ":") print(missing_data) } + df <- df %>% filter(!is.na(!!y_var_sym)) } - + + # Filter out-of-range data if limits_map is provided if (!is.null(limits_map)) { - # Get y-limits for the variable ylim_vals <- limits_map[[variable]] - - # Identify out-of-range data and print it + + # Print and filter out-of-range data out_of_range_data <- df %>% filter( - !is.na(!!y_var_sym) & - (!!y_var_sym < min(ylim_vals, na.rm = TRUE) | !!y_var_sym > max(ylim_vals, na.rm = TRUE)) + !!y_var_sym < ylim_vals[1] | !!y_var_sym > ylim_vals[2] ) if (nrow(out_of_range_data) > 0) { message("Out-of-range data for variable ", variable, ":") print(out_of_range_data) + df <- df %>% + filter(!!y_var_sym >= ylim_vals[1] & !!y_var_sym <= ylim_vals[2]) } } } - - # Filter data by checking if all variables are within the specified limits - if (!is.null(limits_map)) { - df_filtered <- df %>% - filter(if_all(all_of(variables), ~ !is.na(.))) %>% # Check for non-NA values - filter(if_all(all_of(variables), ~ . >= limits_map[[cur_column()]][1] & . <= limits_map[[cur_column()]][2])) - } else { - df_filtered <- df %>% filter(if_all(all_of(variables), ~ !is.na(.))) - } - - return(df_filtered) + + return(df) } main <- function() {