From 37743b1f5e7e47faeced1f957fdc956ded82d281 Mon Sep 17 00:00:00 2001 From: Bryan Roessler Date: Mon, 16 Sep 2024 15:07:07 -0400 Subject: [PATCH] Pull cur_column() from across() --- .../apps/r/calculate_interaction_zscores.R | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R index caef72f6..1753cead 100644 --- a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R +++ b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R @@ -789,43 +789,39 @@ filter_and_print_non_finite <- function(df, vars_to_check, print_vars) { filter_data_for_plots <- function(df, variables, missing = TRUE, limits_map = NULL) { - # Initialize lists to store lm lines - lm_lines <- list() - - # Check for missing and out-of-range data + # Print missing data and out-of-range data separately for (variable in variables) { y_var_sym <- sym(variable) - # Print missing data if requested if (missing) { missing_data <- df %>% filter(is.na(!!y_var_sym)) if (nrow(missing_data) > 0) { - message("Filtering missing data for variable ", variable, " for plotting:") - print(head(missing_data, 10)) # Print only the first 10 rows to avoid too much output + message("Missing data for variable ", variable, ":") + print(missing_data) } } - # Print 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 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)) ) if (nrow(out_of_range_data) > 0) { - message("Filtering out-of-range data for variable ", variable, " for plotting:") - print(head(out_of_range_data, 10)) # Print only the first 10 rows + message("Out-of-range data for variable ", variable, ":") + print(out_of_range_data) } } } - # Apply filtering across all variables in one step using if_any and if_all + # 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(.))) %>% - filter(if_all(all_of(variables), - ~ between(., limits_map[[cur_column()]][1], limits_map[[cur_column()]][2]) - )) + 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(.))) }