fixed the "key insights" calculation

This commit is contained in:
DimitraVeropoulou 2026-02-12 17:27:21 +01:00
parent eb1b7772e5
commit 684050d459

View file

@ -307,66 +307,79 @@ if (!is.null(CI_quadrant) && nrow(CI_quadrant) > 0) {
## Key Insights ## Key Insights
```{r key_insights, echo=FALSE, results='asis'} ```{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) { if (exists("summary_tables") && !is.null(summary_tables) && length(summary_tables) > 0) {
# Extract aggregated KPI summaries (farm-level, not per-field) # Aggregate per-field KPI data into summaries on-the-fly
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
# Total fields analyzed (from uniformity summary) # 1. Uniformity insights - group by interpretation
total_fields <- sum(uniformity_summary$`Field Count`, na.rm = TRUE) if (!is.null(summary_tables$uniformity) && nrow(summary_tables$uniformity) > 0) {
# Uniformity insights
if (!is.null(uniformity_summary) && nrow(uniformity_summary) > 0) {
cat("**Field Uniformity:**\n") cat("**Field Uniformity:**\n")
for (i in 1:nrow(uniformity_summary)) { uniformity_counts <- summary_tables$uniformity %>%
status <- uniformity_summary$Status[i] group_by(interpretation) %>%
count <- uniformity_summary$`Field Count`[i] summarise(count = n(), .groups = 'drop')
if (count > 0) {
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="") cat("- ", count, " field(s) with ", status, "\n", sep="")
} }
} }
} }
# Area change insights # 2. Area change insights - group by interpretation
if (!is.null(area_change_summary) && nrow(area_change_summary) > 0) { if (!is.null(summary_tables$area_change) && nrow(summary_tables$area_change) > 0) {
cat("\n**Area Change Status:**\n") cat("\n**Area Change Status:**\n")
for (i in 1:nrow(area_change_summary)) { area_counts <- summary_tables$area_change %>%
status <- area_change_summary$Status[i] group_by(interpretation) %>%
count <- area_change_summary$`Field Count`[i] summarise(count = n(), .groups = 'drop')
if (count > 0 && !is.na(status)) {
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="") cat("- ", count, " field(s) ", status, "\n", sep="")
} }
} }
} }
# Growth trend insights # 3. Growth trend insights - group by trend_interpretation
if (!is.null(growth_summary) && nrow(growth_summary) > 0) { if (!is.null(summary_tables$growth_decline) && nrow(summary_tables$growth_decline) > 0) {
cat("\n**Growth Trends (4-Week):**\n") cat("\n**Growth Trends (4-Week):**\n")
for (i in 1:nrow(growth_summary)) { growth_counts <- summary_tables$growth_decline %>%
trend <- growth_summary$Trend[i] group_by(trend_interpretation) %>%
count <- growth_summary$`Field Count`[i] summarise(count = n(), .groups = 'drop')
if (count > 0 && !is.na(trend)) {
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="") cat("- ", count, " field(s) with ", trend, "\n", sep="")
} }
} }
} }
# Weed pressure insights # 4. Weed pressure insights - group by weed_pressure_risk
if (!is.null(weed_summary) && nrow(weed_summary) > 0) { if (!is.null(summary_tables$weed_pressure) && nrow(summary_tables$weed_pressure) > 0) {
cat("\n**Weed/Pest Pressure Risk:**\n") cat("\n**Weed/Pest Pressure Risk:**\n")
for (i in 1:nrow(weed_summary)) { weed_counts <- summary_tables$weed_pressure %>%
risk <- weed_summary$`Risk Level`[i] group_by(weed_pressure_risk) %>%
count <- weed_summary$`Field Count`[i] summarise(count = n(), .groups = 'drop')
if (count > 0 && !is.na(risk)) {
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="") 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 { } else {
cat("KPI data not available for ", project_dir, " on this date.\n", sep="") cat("KPI data not available for ", project_dir, " on this date.\n", sep="")
} }