From ed5009b7d10e3fcc92bd18807f37c1dc49a428e0 Mon Sep 17 00:00:00 2001 From: Josh Yamamoto Date: Thu, 20 Jun 2024 15:38:11 -0500 Subject: [PATCH 1/2] allow rowvar & colvar to be set and also allow neither to be set --- R/modGBchng.R | 58 ++++++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/R/modGBchng.R b/R/modGBchng.R index 51343b03..050f8c6f 100644 --- a/R/modGBchng.R +++ b/R/modGBchng.R @@ -267,7 +267,7 @@ modGBchng <- function(GBpopdat, parameters <- FALSE returnlst <- list() rawdata <- TRUE - colvar <- NULL + #colvar <- NULL ## Set global variables ONEUNIT=n.total=n.strata=strwt=TOTAL= @@ -501,46 +501,44 @@ modGBchng <- function(GBpopdat, setkeyv(condx, c(cuniqueid, condid)) setkeyv(condf, c(cuniqueid, condid)) - -# if (rowvar == "TOTAL" || colvar == "NONE") { -# stop("must include a rowvar and a colvar for area change estimates") -# } - -# groupby.qry <- paste0("c.", cuniqueid, ", c.", condid) - groupby.qry <- paste0(toString(paste0("sccm.", strunitvars)), ", c.", cuniqueid, ", c.", condid) -# groupby.qry <- paste0(toString(strunitvars), ", "c.", cuniqueid) + prev_rowvar <- paste0("PREV_", rowvar) + rowvar_keep <- NULL if (rowvar != "TOTAL") { - select.qry <- paste0(groupby.qry, ", pcond.", rowvar, " AS ", prev_rowvar) + if (colvar != "NONE") { - select.qry <- paste0(select.qry, ", c.", colvar, " AS ", colvar) + + select.qry <- paste0(groupby.qry, ", c.", rowvar, " AS ", rowvar, ", c.", colvar, " AS ", colvar) + } else { - + + select.qry <- paste0(groupby.qry, ", pcond.", rowvar, " AS ", prev_rowvar) + ## Copy rowvar information to colvar colvar <- rowvar - colvarnm <- rowvarnm + colvarnm <- rowvarnm uniquecol <- copy(uniquerow) title.colvar <- title.rowvar - if (!is.null(row.orderby)) { - col.orderby <- row.orderby - } + + rowvar_keep <- rowvar + + if (!is.null(row.orderby)) col.orderby <- row.orderby + select.qry <- paste0(select.qry, ", c.", rowvar) ## Add prefix to rowvar names(uniquerow) <- paste0("PREV_", names(uniquerow)) rowvar <- prev_rowvar rowvarnm <- paste0("PREV_", rowvarnm) - if (!is.null(row.orderby)) { - row.orderby <- paste0("PREV_", row.orderby) - } + if (!is.null(row.orderby)) row.orderby <- paste0("PREV_", row.orderby) ## Get group variable for cell values grpvar <- c(rowvar, colvar) } -# } else { -# select.qry <- groupby.qry + } else { + select.qry <- groupby.qry } if (chngtype == "annual") { @@ -564,19 +562,13 @@ modGBchng <- function(GBpopdat, AND COALESCE(c.COND_NONSAMPLE_REASN_CD, 0) = 0 AND COALESCE(pcond.COND_NONSAMPLE_REASN_CD, 0) = 0", "\nGROUP BY ", groupby.qry) - - # condf_chng.qry <- paste0(condf_chng.qry, - # "\nFROM condf c", - # "\nJOIN condf pcond ON (pcond.PLT_CN = c.PREV_PLT_CN)", - # "\nJOIN condx sccm ON (sccm.plt_cn = c.plt_cn - # AND sccm.prev_plt_cn = pcond.plt_cn - # AND sccm.condid = c.condid - # AND sccm.prevcond = pcond.condid)", - # "\nGROUP BY ", groupby.qry) - if (rowvar != "TOTAL") { - condf_chng.qry <- paste0(condf_chng.qry, ", ", rowvar, ", c.", colvar) + if (!is.null(rowvar_keep)) { + condf_chng.qry <- paste0(condf_chng.qry, ", ", rowvar, ", c.", colvar) + } else { + condf_chng.qry <- paste0(condf_chng.qry, ", c.", rowvar, ", c.", colvar) + } } message(condf_chng.qry) @@ -589,7 +581,7 @@ modGBchng <- function(GBpopdat, if (length(unitvars) == 2) { cdomdat[, (unitvar) := paste(cdomdat[[unitvars[1]]], cdomdat[[unitvars[2]]], sep="-")] - strunitvars <- c(unitvar, strvar) + strunitvars <- c(unitvar, strvar) } } else { ## cdomdat From e0a07925a921a2b576a6705c13ac116cc1bd9ffa Mon Sep 17 00:00:00 2001 From: Josh Yamamoto Date: Fri, 28 Jun 2024 09:41:21 -0500 Subject: [PATCH 2/2] allowing totals to work but stepping back from rowvar & colvar --- R/modGBchng.R | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/R/modGBchng.R b/R/modGBchng.R index 050f8c6f..6c929258 100644 --- a/R/modGBchng.R +++ b/R/modGBchng.R @@ -267,7 +267,7 @@ modGBchng <- function(GBpopdat, parameters <- FALSE returnlst <- list() rawdata <- TRUE - #colvar <- NULL + colvar <- NULL ## Set global variables ONEUNIT=n.total=n.strata=strwt=TOTAL= @@ -502,43 +502,38 @@ modGBchng <- function(GBpopdat, setkeyv(condf, c(cuniqueid, condid)) groupby.qry <- paste0(toString(paste0("sccm.", strunitvars)), ", c.", cuniqueid, ", c.", condid) - prev_rowvar <- paste0("PREV_", rowvar) - rowvar_keep <- NULL if (rowvar != "TOTAL") { - + select.qry <- paste0(groupby.qry, ", pcond.", rowvar, " AS ", prev_rowvar) if (colvar != "NONE") { - - select.qry <- paste0(groupby.qry, ", c.", rowvar, " AS ", rowvar, ", c.", colvar, " AS ", colvar) - + select.qry <- paste0(select.qry, ", c.", colvar, " AS ", colvar) } else { - select.qry <- paste0(groupby.qry, ", pcond.", rowvar, " AS ", prev_rowvar) - ## Copy rowvar information to colvar colvar <- rowvar - colvarnm <- rowvarnm + colvarnm <- rowvarnm uniquecol <- copy(uniquerow) title.colvar <- title.rowvar - - rowvar_keep <- rowvar - - if (!is.null(row.orderby)) col.orderby <- row.orderby - + if (!is.null(row.orderby)) { + col.orderby <- row.orderby + } select.qry <- paste0(select.qry, ", c.", rowvar) - + ## Add prefix to rowvar names(uniquerow) <- paste0("PREV_", names(uniquerow)) rowvar <- prev_rowvar rowvarnm <- paste0("PREV_", rowvarnm) - if (!is.null(row.orderby)) row.orderby <- paste0("PREV_", row.orderby) - + if (!is.null(row.orderby)) { + row.orderby <- paste0("PREV_", row.orderby) + } + ## Get group variable for cell values grpvar <- c(rowvar, colvar) } + } else { - select.qry <- groupby.qry + select.qry <- groupby.qry } if (chngtype == "annual") { @@ -564,11 +559,7 @@ modGBchng <- function(GBpopdat, "\nGROUP BY ", groupby.qry) if (rowvar != "TOTAL") { - if (!is.null(rowvar_keep)) { - condf_chng.qry <- paste0(condf_chng.qry, ", ", rowvar, ", c.", colvar) - } else { - condf_chng.qry <- paste0(condf_chng.qry, ", c.", rowvar, ", c.", colvar) - } + condf_chng.qry <- paste0(condf_chng.qry, ", ", rowvar, ", c.", colvar) } message(condf_chng.qry)