.report.name <- 'dmu-diff'
    .report.version <- '0.7'
    .report.description <- 'Differences between select DMU'
library(knitr, quietly=TRUE)

# chunk options
opts_knit$set(message=FALSE, warning=FALSE, verbose=FALSE, progress=FALSE)

# R session options
options(width=100, stringsAsFactors=FALSE)

## load dependencies
library(daff, quietly=TRUE)
library(aqp, quietly = TRUE)
library(soilDB, quietly=TRUE)

## load report-specific functions
source('custom.R') 

# project metadata: defined in custom.R
project.metadata <- get_project_meta()


## abstract to shared function for all QA reports

# DMU / components as SPC
x <- fetchNASIS(from='components')

# get correlation data so we can group via new map unit name
nc <- get_component_correlation_data_from_NASIS_db(dropNotRepresentative = FALSE)
site(x) <- nc

# establish DMU vintage
x$vintage <- rep('old', times=length(x))

# flag new DMU via repdmu, joined-in from correlation data
x$vintage[which(x$repdmu == 1)] <- 'new'

# unique component name (compname + localphase)
# need this for rare DMU where there are > 1 component with the same name
x$compname_phase <- sprintf("%s%s", x$compname, ifelse(is.na(x$localphase), '', sprintf("-%s", x$localphase)))


## site-level diff
s <- site(x)

# remove IDs from diff
exclude.vars <- c('coiid', 'dmuiid')
site.vars <- which(! names(s) %in% exclude.vars)


## differences must be done by map unit / component
## MU/compname doesn't work when there are >1 components with the same name (not common)
## TODO: use compname + localphase here too 

# split by combination
l <- split(s, list(s$muname, s$compname_phase))

site.diff <- lapply(l, function(i) {

  # extract 'old' vs. 'new' records
  a <- subset(i[, site.vars], subset=vintage == 'old')
  b <- subset(i[, site.vars], subset=vintage == 'new')

  # exclude vars used for grouping
  a$muname <- NULL ; a$areasmybol <- NULL ; a$repdmu <- NULL ; a$muiid <- NULL ; a$vintage <- NULL ; a$dmudesc <- NULL
  b$muname <- NULL; b$areasmybol <- NULL ; b$repdmu <- NULL ; b$muiid <- NULL ; b$vintage <- NULL ; b$dmudesc <- NULL

  # name for each difference
  diff.name <- sprintf('%s/%s', unique(i$muname), unique(i$compname_phase))

  ## copmute and render diff
  delta <- diff_data(a, b)
  res <- render_diff(delta, fragment=TRUE, view = FALSE, title = diff.name, summary = TRUE)

  return(list(title=diff.name, html=res))
})



## hz-level diff
d <- as(x, 'data.frame')

# must include map unit name for grouped comparisons
hz.vars <- c('muname', 'vintage', 'dmudesc', 'compname', 'compname_phase', horizonNames(x))
exclude.vars <- c('coiid', 'chiid', 'hzID')
hz.vars <- hz.vars[which(! hz.vars %in% exclude.vars)]
d <- d[, hz.vars]



## TODO: fix this
## this no longer works: ordering by component pct
# split by MU/component names
# comp.order <- names(sort(tapply(s$comppct_r, s$compname, mean), decreasing = TRUE))
# d$compname <- factor(d$compname, levels=comp.order)

d$muname <- factor(d$muname)
l <- split(d, list(d$muname, d$compname_phase))

hz.diff <- lapply(l, function(i) {

  a <- subset(i, subset=vintage == 'old')
  b <- subset(i, subset=vintage == 'new')

  # exclude vars used for grouping
  a$localphase <- NULL; a$compname <- NULL ; a$comppct_r <- NULL ; a$dmudesc <- NULL ; a$muname <- NULL ; a$vintage <- NULL
  b$localphase <- NULL; b$compname <- NULL ; b$comppct_r <- NULL ; b$dmudesc <- NULL ; b$muname <- NULL ; b$vintage <- NULL

  delta <- diff_data(a, b)

  diff.name <- sprintf('%s/%s', unique(i$muname), unique(i$compname_phase))

  res <- render_diff(delta, fragment=TRUE, view = FALSE, title = diff.name, summary = TRUE)
  return(list(title=diff.name, html=res))

})


DMU Differences

r project.metadata$projectname


report version r .report.version
r format(Sys.time(), "%Y-%m-%d")


This report provides text differencing capabilities, applied to DMU/component records and component/horizon records queried from the selected set. Differences are higlighted in blue, deletions are in red, and additions are in green.

Component

wzxhzdk:2

Horizon

wzxhzdk:3

This document is based on soilDB version r utils::packageDescription("soilDB", field="Version"), and daff version r utils::packageDescription("daff", field="Version").
Report configuration and source code are hosted on GitHub.



ncss-tech/soilReports documentation built on April 25, 2024, 1:03 a.m.