SmartCane/r_app/packages.R
Timon bb2a599075 Enhanced SmartCane executive summary report with explanatory text and fixed priority map coloring
Added explanatory text for all visualizations
Fixed priority map color scheme (red=high priority, green=low priority)
Improved error handling in farm health data calculations
Added fallback mechanisms for missing data
2025-04-23 09:47:19 +02:00

117 lines
3.8 KiB
R

# packages.R
#
# PACKAGE MANAGEMENT FOR SMARTCANE
# ===============================
# This script centralizes all package dependencies for the SmartCane project.
# It installs missing packages and loads all required libraries.
#
#' Check and install packages if needed
#'
#' @param pkg_list List of packages to check and install
#' @param install_missing Whether to install missing packages
#' @return Vector of packages that couldn't be installed (if any)
#'
check_and_install_packages <- function(pkg_list, install_missing = TRUE) {
# Check which packages are already installed
is_installed <- pkg_list %in% rownames(installed.packages())
missing_pkgs <- pkg_list[!is_installed]
# Install missing packages if requested
failed_pkgs <- character(0)
if (length(missing_pkgs) > 0) {
if (install_missing) {
message("Installing ", length(missing_pkgs), " missing packages...")
for (pkg in missing_pkgs) {
tryCatch({
install.packages(pkg, repos = "https://cran.rstudio.com/", dependencies = TRUE)
message(" Installed: ", pkg)
}, error = function(e) {
warning("Failed to install package: ", pkg)
warning("Error: ", e$message)
failed_pkgs <<- c(failed_pkgs, pkg)
})
}
} else {
message("The following packages are required but not installed:")
message(paste(missing_pkgs, collapse = ", "))
failed_pkgs <- missing_pkgs
}
} else {
message("All required packages are already installed.")
}
return(failed_pkgs)
}
#' Load all required packages for SmartCane project
#'
#' @param verbose Whether to show messages during loading
#' @return Logical indicating success (TRUE if all packages loaded)
#'
load_smartcane_packages <- function(verbose = FALSE) {
# Define all required packages
required_packages <- c(
# Geospatial packages
"sf", # Simple Features for spatial vector data
"terra", # Raster data processing
"exactextractr", # Fast extraction from rasters
"tmap", # Thematic mapping for spatial visualization
# Data manipulation
"tidyverse", # Collection of data manipulation packages
"lubridate", # Date manipulation
"readxl", # Excel file reading
"stringr", # String manipulation
"purrr", # Functional programming tools
"zoo", # Time series processing with rolling functions
# Visualization
"ggplot2", # Advanced plotting
"leaflet", # Interactive maps
"plotly", # Interactive plots
# Machine learning and statistics
"caret", # Classification and regression training
"rsample", # Data sampling for modeling
"randomForest", # Random forest implementation
"CAST", # Feature selection for spatial data
# Project management
"here", # Path handling
# Document generation
"knitr", # Dynamic report generation
"rmarkdown" # R Markdown processing
)
# Check and install missing packages
failed_pkgs <- check_and_install_packages(required_packages)
# Load all installed packages
success <- TRUE
for (pkg in setdiff(required_packages, failed_pkgs)) {
tryCatch({
if (verbose) message("Loading package: ", pkg)
suppressPackageStartupMessages(library(pkg, character.only = TRUE))
}, error = function(e) {
warning("Failed to load package: ", pkg)
warning("Error: ", e$message)
success <- FALSE
})
}
# Report any issues
if (length(failed_pkgs) > 0) {
warning("The following packages could not be installed: ",
paste(failed_pkgs, collapse = ", "))
success <- FALSE
}
return(success)
}
# Run the loading function if the script is sourced directly
if (!exists("skip_package_loading") || !skip_package_loading) {
load_smartcane_packages()
}