From 684050d459cb8544002ef00becf1c589a64ac7d6 Mon Sep 17 00:00:00 2001 From: DimitraVeropoulou Date: Thu, 12 Feb 2026 17:27:21 +0100 Subject: [PATCH] fixed the "key insights" calculation --- ..._CI_report_with_kpis_agronomic_support.Rmd | 79 +++++++++++-------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/r_app/90_CI_report_with_kpis_agronomic_support.Rmd b/r_app/90_CI_report_with_kpis_agronomic_support.Rmd index 2d59d0d..33451e9 100644 --- a/r_app/90_CI_report_with_kpis_agronomic_support.Rmd +++ b/r_app/90_CI_report_with_kpis_agronomic_support.Rmd @@ -307,66 +307,79 @@ if (!is.null(CI_quadrant) && nrow(CI_quadrant) > 0) { ## Key Insights ```{r key_insights, echo=FALSE, results='asis'} -# Calculate key insights from aggregated KPI summary data +# Calculate key insights from KPI data if (exists("summary_tables") && !is.null(summary_tables) && length(summary_tables) > 0) { - # Extract aggregated KPI summaries (farm-level, not per-field) - uniformity_summary <- summary_tables$uniformity # Has: Status, Field Count, Avg CV, Avg Moran's I - area_change_summary <- summary_tables$area_change # Has: Status, Field Count, Avg CI Change % - growth_summary <- summary_tables$growth_decline # Has: Trend, Field Count, Avg 4-Week Trend - weed_summary <- summary_tables$weed_pressure # Has: Risk Level, Field Count, Avg Fragmentation + # Aggregate per-field KPI data into summaries on-the-fly - # Total fields analyzed (from uniformity summary) - total_fields <- sum(uniformity_summary$`Field Count`, na.rm = TRUE) - - # Uniformity insights - if (!is.null(uniformity_summary) && nrow(uniformity_summary) > 0) { + # 1. Uniformity insights - group by interpretation + if (!is.null(summary_tables$uniformity) && nrow(summary_tables$uniformity) > 0) { cat("**Field Uniformity:**\n") - for (i in 1:nrow(uniformity_summary)) { - status <- uniformity_summary$Status[i] - count <- uniformity_summary$`Field Count`[i] - if (count > 0) { + uniformity_counts <- summary_tables$uniformity %>% + group_by(interpretation) %>% + summarise(count = n(), .groups = 'drop') + + for (i in 1:nrow(uniformity_counts)) { + status <- uniformity_counts$interpretation[i] + count <- uniformity_counts$count[i] + if (!is.na(status) && !is.na(count) && count > 0) { cat("- ", count, " field(s) with ", status, "\n", sep="") } } } - # Area change insights - if (!is.null(area_change_summary) && nrow(area_change_summary) > 0) { + # 2. Area change insights - group by interpretation + if (!is.null(summary_tables$area_change) && nrow(summary_tables$area_change) > 0) { cat("\n**Area Change Status:**\n") - for (i in 1:nrow(area_change_summary)) { - status <- area_change_summary$Status[i] - count <- area_change_summary$`Field Count`[i] - if (count > 0 && !is.na(status)) { + area_counts <- summary_tables$area_change %>% + group_by(interpretation) %>% + summarise(count = n(), .groups = 'drop') + + for (i in 1:nrow(area_counts)) { + status <- area_counts$interpretation[i] + count <- area_counts$count[i] + if (!is.na(status) && !is.na(count) && count > 0) { cat("- ", count, " field(s) ", status, "\n", sep="") } } } - # Growth trend insights - if (!is.null(growth_summary) && nrow(growth_summary) > 0) { + # 3. Growth trend insights - group by trend_interpretation + if (!is.null(summary_tables$growth_decline) && nrow(summary_tables$growth_decline) > 0) { cat("\n**Growth Trends (4-Week):**\n") - for (i in 1:nrow(growth_summary)) { - trend <- growth_summary$Trend[i] - count <- growth_summary$`Field Count`[i] - if (count > 0 && !is.na(trend)) { + growth_counts <- summary_tables$growth_decline %>% + group_by(trend_interpretation) %>% + summarise(count = n(), .groups = 'drop') + + for (i in 1:nrow(growth_counts)) { + trend <- growth_counts$trend_interpretation[i] + count <- growth_counts$count[i] + if (!is.na(trend) && !is.na(count) && count > 0) { cat("- ", count, " field(s) with ", trend, "\n", sep="") } } } - # Weed pressure insights - if (!is.null(weed_summary) && nrow(weed_summary) > 0) { + # 4. Weed pressure insights - group by weed_pressure_risk + if (!is.null(summary_tables$weed_pressure) && nrow(summary_tables$weed_pressure) > 0) { cat("\n**Weed/Pest Pressure Risk:**\n") - for (i in 1:nrow(weed_summary)) { - risk <- weed_summary$`Risk Level`[i] - count <- weed_summary$`Field Count`[i] - if (count > 0 && !is.na(risk)) { + weed_counts <- summary_tables$weed_pressure %>% + group_by(weed_pressure_risk) %>% + summarise(count = n(), .groups = 'drop') + + for (i in 1:nrow(weed_counts)) { + risk <- weed_counts$weed_pressure_risk[i] + count <- weed_counts$count[i] + if (!is.na(risk) && !is.na(count) && count > 0) { cat("- ", count, " field(s) at ", risk, " risk\n", sep="") } } } + # 5. Total fields analyzed + total_fields <- nrow(summary_tables$uniformity) + cat("\n**Total Fields Analyzed:** ", total_fields, "\n", sep="") + } else { cat("KPI data not available for ", project_dir, " on this date.\n", sep="") }