diff --git a/.gitignore b/.gitignore index cf5b8fc..12458df 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,8 @@ Rplots.pdf # R Data Files *.rds +*.here +*.png !renv.lock # Data Files (Excel, CSV, Text) 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 4b63f09..9513e0c 100644 --- a/r_app/90_CI_report_with_kpis_agronomic_support.Rmd +++ b/r_app/90_CI_report_with_kpis_agronomic_support.Rmd @@ -3,7 +3,7 @@ params: ref: "word-styles-reference-var1.docx" output_file: "CI_report.docx" report_date: "2026-02-04" #!r Sys.Date() - data_dir: "aura" + data_dir: "tpc" mail_day: "Wednesday" borders: FALSE ci_plot_type: "both" @@ -502,14 +502,11 @@ tryCatch({ t <- function(key) { if (key %in% names(tr)) { txt <- glue(tr[key], .envir = parent.frame()) - - # 1. Handle tabs - txt <- gsub(">>", " ", txt) - - # 2. Handle newlines txt <- gsub("\n", " \n", txt) - return(txt) + return(enc2utf8(as.character(txt))) + } else if (key == "" | is.na(key)) { + return("") } else { return(paste0("[", key, "]")) } @@ -518,13 +515,11 @@ t <- function(key) { ::: {custom-style="Cover_title" style="text-align:center; margin-top:120px;"} -Satellite Based Field Reporting +`r t("cover_title")` ::: - - ::: {custom-style="Cover_subtitle" style="text-align:center; margin-top:18px;"} -Chlorophyll Index (CI) Monitoring Report — `r toupper(params$data_dir)` Farm (Week `r { rd <- params$report_date; rd <- if (inherits(rd, "Date")) rd else suppressWarnings(as.Date(rd)); if (is.na(rd)) rd <- Sys.Date(); if (!is.null(params$week)) params$week else format(rd, '%V') }`, `r { rd <- params$report_date; rd <- if (inherits(rd, "Date")) rd else suppressWarnings(as.Date(rd)); if (is.na(rd)) rd <- Sys.Date(); format(rd, '%Y') }`) +`r t("cover_subtitle")` ::: \newpage @@ -543,7 +538,7 @@ if (exists("summary_tables") && !is.null(summary_tables) && length(summary_table # 1. Uniformity insights - group by interpretation if (!is.null(summary_tables$uniformity) && nrow(summary_tables$uniformity) > 0) { - cat(t("field_unif")) + cat(t("field_unif"), "\n\n") uniformity_counts <- summary_tables$uniformity %>% dplyr::select(interpretation, count = field_count) @@ -551,14 +546,14 @@ if (exists("summary_tables") && !is.null(summary_tables) && length(summary_table status <- uniformity_counts$interpretation[i] count <- uniformity_counts$count[i] if (!is.na(status) && !is.na(count) && count > 0) { - cat(t("unif_status")) + cat("-", t("unif_status"), "\n\n") } } } # 2. Area change insights - group by interpretation if (!is.null(summary_tables$area_change) && nrow(summary_tables$area_change) > 0) { - cat("\n", t("field_area")) + cat(t("field_area"), "\n\n") area_counts <- summary_tables$area_change %>% dplyr::select(interpretation, count = field_count) @@ -566,14 +561,14 @@ if (exists("summary_tables") && !is.null(summary_tables) && length(summary_table status <- area_counts$interpretation[i] count <- area_counts$count[i] if (!is.na(status) && !is.na(count) && count > 0) { - cat(t("area_status")) + cat("-", t("area_status"), "\n\n") } } } # 3. Growth trend insights - group by trend_interpretation if (!is.null(summary_tables$growth_decline) && nrow(summary_tables$growth_decline) > 0) { - cat("\n", t("growth_trend")) + cat(t("growth_trend"), "\n\n") growth_counts <- summary_tables$growth_decline %>% dplyr::select(trend_interpretation, count = field_count) @@ -581,14 +576,14 @@ if (exists("summary_tables") && !is.null(summary_tables) && length(summary_table trend <- growth_counts$trend_interpretation[i] count <- growth_counts$count[i] if (!is.na(trend) && !is.na(count) && count > 0) { - cat(t("trend_status")) + cat("-", t("trend_status"), "\n\n") } } } # 4. Patchiness insights - group by patchiness_risk if (!is.null(summary_tables$patchiness) && nrow(summary_tables$patchiness) > 0) { - cat("\n", t("patch_risk")) + cat(t("patch_risk"), "\n\n") patchiness_counts <- summary_tables$patchiness %>% dplyr::select(patchiness_risk, count = field_count) @@ -596,14 +591,14 @@ if (exists("summary_tables") && !is.null(summary_tables) && length(summary_table risk <- patchiness_counts$patchiness_risk[i] count <- patchiness_counts$count[i] if (!is.na(risk) && !is.na(count) && count > 0) { - cat(t("patch_status")) + cat("-", t("patch_status"), "\n\n") } } } # 5. Total fields analyzed total_fields <- sum(summary_tables$uniformity$field_count, na.rm = TRUE) - cat("\n", t("tot_fields_analyzed")) + cat(t("tot_fields_analyzed")) } else { cat(t("kpi_na")) @@ -614,7 +609,7 @@ if (exists("summary_tables") && !is.null(summary_tables) && length(summary_table `r t("section_i")` -`r t("exec_summary")` +`r t("exec_summary")`\n\n ```{r combined_kpi_table, echo=FALSE, results='asis'} # Display KPI tables - standardized format with Level, Count, Percent columns @@ -692,8 +687,12 @@ if (exists("summary_tables") && !is.null(summary_tables) && length(summary_table display_df <- combined_df %>% dplyr::select(KPI = KPI_display, Level, Count, Percent) + # Translate the table for visualization + names(display_df) <- c(t("KPI"), t("Level"), t("Count"), t("Percent")) + display_df[, 1:2] <- lapply(display_df[, 1:2], function(col) sapply(col, t)) + ft <- flextable(display_df) %>% - merge_v(j = "KPI") %>% + merge_v(j = t("KPI")) %>% autofit() cum_rows <- cumsum(kpi_group_sizes) @@ -703,15 +702,15 @@ if (exists("summary_tables") && !is.null(summary_tables) && length(summary_table hline(i = cum_rows[i], border = officer::fp_border(width = 2)) } } - + ft } else { - cat("No valid KPI summary tables found for display.\n") + cat(t("no_kpi_table")) } }, error = function(e) { safe_log(paste("Error displaying KPI tables:", e$message), "WARNING") - cat("KPI summary tables could not be displayed. Individual KPI sections will be shown below.\n") + cat(t("kpi_table_error")) }) } else { @@ -730,7 +729,7 @@ generate_field_alerts <- function(field_details_table) { } # Check for required columns - required_cols <- c("Field", "Field Size (acres)", "Growth Uniformity", "Yield Forecast (t/ha)", + required_cols <- c("Field", "Field Size (hectares)", "Growth Uniformity", "Yield Forecast (t/ha)", "Gap Score", "Decline Risk", "Patchiness Risk", "Mean CI", "CV Value", "Moran's I") missing_cols <- setdiff(required_cols, colnames(field_details_table)) @@ -887,7 +886,6 @@ if (!exists("CI_quadrant")) { } ``` - ```{r load_field_boundaries, message=TRUE, warning=TRUE, include=FALSE} # Load field boundaries from GeoJSON tryCatch({ @@ -1116,9 +1114,9 @@ tryCatch({ # Choose color palette based on colorblind_friendly parameter if (colorblind_friendly) { fill_scale <- ggplot2::scale_fill_viridis_c( - name = "Chlorophyll Index (CI)", + name = t("legend_ci"), limits = c(1, 8), - direction = -1, # Reversed: green=high, yellow/red=low + direction = 1, na.value = "transparent", oob = scales::squish ) @@ -1126,7 +1124,7 @@ tryCatch({ # Use Red-Yellow-Green diverging palette (reversed for intuitive interpretation) fill_scale <- ggplot2::scale_fill_distiller( palette = "RdYlGn", - name = "Chlorophyll Index (CI)", + name = t("legend_ci"), limits = c(1, 8), direction = 1, # Standard direction for RdYlGn na.value = "transparent" @@ -1190,7 +1188,7 @@ tryCatch({ panel.background = ggplot2::element_rect(fill = "white", color = NA) ) + ggplot2::labs( - title = paste("Current Week CI Overview - Week", current_week, "of", current_iso_year) + title = t("ci_overview_title") ) # Print the map @@ -1209,7 +1207,7 @@ tryCatch({ }) ``` -```{r render_farm_ci_diff_map, echo=FALSE, message=FALSE, warning=FALSE, fig.height=6.8, fig.width=8.5, dpi=150, dev='png'} +```{r render_farm_ci_diff_map, echo=FALSE, fig.height=6.8, fig.width=8.5, message=FALSE, warning=FALSE, dev='png', dpi=150} # Create farm-level CI difference map (week-over-week change) tryCatch({ if (!is.null(farm_ci_diff_week_ll)) { @@ -1223,7 +1221,7 @@ tryCatch({ if (colorblind_friendly) { # Use plasma for colorblind-friendly diverging visualization fill_scale <- ggplot2::scale_fill_viridis_c( - name = "CI Change (Week-over-Week)", + name = t("legend_ci_change"), option = "plasma", limits = c(-3, 3), na.value = "transparent", @@ -1233,7 +1231,7 @@ tryCatch({ # Use Red-Blue diverging palette (red=decline, blue=increase) fill_scale <- ggplot2::scale_fill_distiller( palette = "RdBu", - name = "CI Change (Week-over-Week)", + name = t("legend_ci_change"), limits = c(-3, 3), direction = 1, na.value = "transparent" @@ -1297,7 +1295,7 @@ tryCatch({ panel.background = ggplot2::element_rect(fill = "white", color = NA) ) + ggplot2::labs( - title = paste("Weekly CI Change - Week", current_week, "vs Week", week_minus_1) + title = t("ci_change_title") ) # Print the map @@ -1316,18 +1314,9 @@ tryCatch({ }) ``` -# Section 2: Field-by-Field Analysis +\newpage -## Overview of Field-Level Insights -This section provides detailed, field-specific analyses including chlorophyll index maps, trend graphs, and performance metrics. Each field is analyzed individually to support targeted interventions. - -**Key Elements per Field:** -- Current and historical CI maps -- Week-over-week change visualizations -- Cumulative growth trends -- Field-specific KPI summaries - -*Navigate to the following pages for individual field reports.* +`r t("section_ii")` \newpage @@ -1516,7 +1505,7 @@ tryCatch({ }) ``` -```{r generate_subarea_visualizations, echo=FALSE, fig.height=3.8, fig.width=6.5, dpi=150, message=FALSE, warning=FALSE, results='asis', eval=FALSE} +```{r generate_subarea_visualizations, eval=FALSE, echo=FALSE, fig.height=3.8, fig.width=6.5, message=FALSE, warning=FALSE, dpi=150, results='asis'} # Alternative visualization grouped by sub-area (disabled by default) tryCatch({ # Group pivots by sub-area @@ -1549,17 +1538,14 @@ tryCatch({ }) ``` -\newpage -## Detailed Field Performance Summary by Field - -The following table provides a comprehensive overview of all monitored fields with their key performance metrics from the KPI analysis. +`r t("detailed_field")` ```{r detailed_field_table, echo=FALSE, results='asis'} # Detailed field performance table if (!exists("field_details_table") || is.null(field_details_table) || nrow(field_details_table) == 0) { safe_log("No field details available for table", "WARNING") - cat("No field-level KPI data available for this report period.\n") + cat(t("no_field_data")) } else { # Calculate field sizes from boundaries (convert to acres) @@ -1613,33 +1599,56 @@ if (!exists("field_details_table") || is.null(field_details_table) || nrow(field field_details_clean <- field_details_clean %>% mutate(Weekly_CI_Change = round(Weekly_CI_Change, 2)) %>% select( - Field = Field_id, - `Field Size (acres)` = field_size_acres, - `Mean CI` = Mean_CI, - `Weekly CI Change` = Weekly_CI_Change, - `Yield Forecast (t/ha)` = TCH_Forecasted, - `Gap Score %` = Gap_Score, - `Decline Risk` = Decline_Severity, - `Patchiness Risk` = Patchiness_Risk, - `CV Value` = CV + field = Field_id, + field_size = field_size_acres, + mean_ci = Mean_CI, + weekly_ci_change = Weekly_CI_Change, + yield_forecast = TCH_Forecasted, + gap_score = Gap_Score, + decline_risk = Decline_Severity, + patchiness_risk = Patchiness_Risk, + cv_value = CV ) } else { field_details_clean <- field_details_clean %>% select( - Field = Field_id, - `Field Size (acres)` = field_size_acres, - `Mean CI` = Mean_CI, - `Yield Forecast (t/ha)` = TCH_Forecasted, - `Gap Score %` = Gap_Score, - `Decline Risk` = Decline_Severity, - `Patchiness Risk` = Patchiness_Risk, - `CV Value` = CV + field = Field_id, + field_size = field_size_acres, + mean_ci = Mean_CI, + yield_forecast = TCH_Forecasted, + gap_score = Gap_Score, + decline_risk = Decline_Severity, + patchiness_risk = Patchiness_Risk, + cv_value = CV ) } + # Translate risk levels + field_details_clean <- field_details_clean %>% + mutate( + across( + c(decline_risk, patchiness_risk), + ~ sapply(.x, t) + ) + ) + + # Translation labels for flextable + header_labels <- list( + field = t("field"), + field_size = t("field_size"), + mean_ci = t("mean_ci"), + weekly_ci_change = t("weekly_ci_change"), + yield_forecast = t("yield_forecast"), + gap_score = t("gap_score"), + decline_risk = t("decline_risk"), + patchiness_risk = t("patchiness_risk"), + cv_value = t("cv_value") + ) + # Display the cleaned field table with flextable (fit to page width) ft <- flextable(field_details_clean) %>% - set_caption("Detailed Field Performance Summary") %>% + set_header_labels(values = header_labels) %>% + set_caption(t("detailed_field_caption")) %>% theme_booktabs() %>% set_table_properties(width = 1, layout = "autofit") # Fit to 100% page width with auto-adjust @@ -1649,144 +1658,101 @@ if (!exists("field_details_table") || is.null(field_details_table) || nrow(field \newpage -This automated report provides weekly analysis of sugarcane crop health using satellite-derived Chlorophyll Index (CI) measurements. The analysis supports: +`r t("section_iii")` -• Scouting of growth related issues that are in need of attention -• Timely actions can be taken such that negative impact is reduced -• Monitoring of the crop growth rates of the farm, providing evidence of performance -• Planning of harvest moment and mill logistics is supported such that optimal tonnage and sucrose levels can be harvested. - -The base of the report is the Chlorophyll Index. The chlorophyll index identifies: -• Field-level crop health variations => target problem area's -• Weekly changes in crop vigor => scout for diseases and stress -• Areas requiring attention by the agricultural and irrigation teams -• Growth patterns across different field sections - -Key Features: - High-resolution satellite imagery analysis - Week-over-week change detection - Individual field performance metrics - Actionable insights for crop management - -### Explanation of the Report - -This report provides a detailed analysis (3x3m of resolution) of your sugarcane fields based on satellite imagery. It supports you monitor crop health and development throughout the growing season. The data is processed weekly to give you timely insights for optimal farm management decisions. - -### What is the Chlorophyll Index (CI)? - -The Chlorophyll Index (CI) is a vegetation index that measures the relative amount of chlorophyll in plant leaves. Chlorophyll is the green pigment responsible for photosynthesis in plants. Higher CI values indicate: -• Greater photosynthetic activity -• Healthier plant tissue -• Better nitrogen uptake -• More vigorous crop growth - -CI values typically range from 0 (bare soil or severely stressed vegetation) to 7+ (very healthy, dense vegetation). For sugarcane, values between 3-7 generally indicate good crop health, depending on the growth stage. +```{r include=FALSE} +# Tries to get the CI graph in different language, otherwise falls back on English +target_img <- paste0("CI_graph_example_", lang, ".png") +img_path <- ifelse(file.exists(target_img), target_img, "CI_graph_example.png") +```