## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse  = TRUE,
  comment   = "#>",
  fig.width = 7,
  fig.height = 4
)

## ----load---------------------------------------------------------------------
library(soiltillr)

## ----datasets-----------------------------------------------------------------
data(tillage_operations)
data(erosion_profile)

head(tillage_operations)
head(erosion_profile)

## ----validate-----------------------------------------------------------------
chk <- validate_soil_data(
  data      = tillage_operations,
  year_col  = "year",
  field_col = "field_id",
  value_col = "depth_cm"
)

chk$valid    # TRUE = data is clean
chk$issues   # critical errors
chk$warnings # informational notices

## ----summarise----------------------------------------------------------------
till_sum <- summarise_tillage(
  data      = tillage_operations,
  year_col  = "year",
  field_col = "field_id",
  depth_col = "depth_cm",
  op_col    = "operation"
)
print(till_sum)

## ----trend--------------------------------------------------------------------
till_trend <- tillage_depth_trend(
  data      = tillage_operations,
  year_col  = "year",
  field_col = "field_id",
  depth_col = "depth_cm"
)
print(till_trend)

# Years with decreasing tillage depth
till_trend[till_trend$trend == "decreasing", ]

## ----compaction---------------------------------------------------------------
risk <- detect_compaction(
  data                    = tillage_operations,
  year_col                = "year",
  field_col               = "field_id",
  depth_col               = "depth_cm",
  op_col                  = "operation",
  compaction_threshold_cm = 20
)
print(risk)

# High-risk years only
risk[risk$compaction_risk == "high", ]

## ----erosion_track------------------------------------------------------------
eros <- track_erosion_depth(
  data        = erosion_profile,
  year_col    = "year",
  field_col   = "field_id",
  erosion_col = "erosion_depth_mm"
)
print(eros)

## ----soil_loss----------------------------------------------------------------
loss <- estimate_soil_loss(
  data             = erosion_profile,
  year_col         = "year",
  field_col        = "field_id",
  erosion_col      = "erosion_depth_mm",
  bulk_density_col = "bulk_density_g_cm3",
  slope_col        = "slope_pct"
)
print(loss)

## ----compare------------------------------------------------------------------
comp <- compare_fields(
  data        = erosion_profile,
  year_col    = "year",
  field_col   = "field_id",
  erosion_col = "erosion_depth_mm",
  om_col      = "organic_matter_pct"
)
print(comp)

## ----plot_tillage, fig.cap = "Mean annual tillage depth by field, 2018-2023."----
plot_tillage_timeline(
  data      = tillage_operations,
  year_col  = "year",
  field_col = "field_id",
  depth_col = "depth_cm",
  title     = "Tillage Depth Transition: Conventional to Conservation"
)

## ----plot_erosion, fig.cap = "Annual erosion depth and cumulative loss by field."----
plot_erosion_trend(
  data            = erosion_profile,
  year_col        = "year",
  field_col       = "field_id",
  erosion_col     = "erosion_depth_mm",
  show_cumulative = TRUE,
  title           = "Erosion Depth and Cumulative Loss 2018-2023"
)

## ----plot_om, fig.cap = "Soil organic matter recovery under conservation tillage."----
plot_om_trend(
  data      = erosion_profile,
  year_col  = "year",
  field_col = "field_id",
  om_col    = "organic_matter_pct",
  title     = "Soil Organic Matter Recovery"
)

## ----plot_comparison, fig.cap = "Dual-panel comparison of mean tillage depth and erosion depth."----
plot_tillage_erosion(
  tillage_data = tillage_operations,
  erosion_data = erosion_profile,
  year_col     = "year",
  field_col    = "field_id",
  depth_col    = "depth_cm",
  erosion_col  = "erosion_depth_mm",
  title        = "Impact of Tillage Management on Soil Erosion"
)

## ----pipeline, eval = FALSE---------------------------------------------------
#  library(soiltillr)
#  data(tillage_operations)
#  data(erosion_profile)
#  
#  # Step 1: validate
#  stopifnot(validate_soil_data(tillage_operations,
#                               "year", "field_id", "depth_cm")$valid)
#  
#  # Step 2: tillage
#  till_sum   <- summarise_tillage(tillage_operations, "year", "field_id",
#                                  "depth_cm", op_col = "operation")
#  till_trend <- tillage_depth_trend(tillage_operations, "year",
#                                    "field_id", "depth_cm")
#  compact    <- detect_compaction(tillage_operations, "year", "field_id",
#                                  "depth_cm", op_col = "operation")
#  
#  # Step 3: erosion
#  eros_track <- track_erosion_depth(erosion_profile, "year",
#                                    "field_id", "erosion_depth_mm")
#  eros_loss  <- estimate_soil_loss(erosion_profile, "year", "field_id",
#                                   "erosion_depth_mm", "bulk_density_g_cm3",
#                                   slope_col = "slope_pct")
#  field_comp <- compare_fields(erosion_profile, "year", "field_id",
#                                "erosion_depth_mm", "organic_matter_pct")
#  
#  # Step 4: visualise
#  plot_tillage_timeline(tillage_operations, "year", "field_id", "depth_cm")
#  plot_erosion_trend(erosion_profile, "year", "field_id",
#                     "erosion_depth_mm", show_cumulative = TRUE)
#  plot_om_trend(erosion_profile, "year", "field_id", "organic_matter_pct")
#  plot_tillage_erosion(tillage_operations, erosion_profile,
#                       "year", "field_id", "depth_cm", "erosion_depth_mm")

