Skip to content

Commit

Permalink
get_ecosite_history_from_NASIS_db: allow custom filtering on ES class…
Browse files Browse the repository at this point in the history
…ifier field; thanks to @natearoe
  • Loading branch information
brownag committed Jan 23, 2024
1 parent 3c557a4 commit be15aba
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
5 changes: 3 additions & 2 deletions R/get_ecosite_history_from_NASIS_db.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
#'
#' @param best Should the "best" ecological site correlation be chosen? Creates field called `es_selection_method` with `"most recent"` or `"least missing data"` for resolving many:1 relationships in site history.
#' @param SS Use selected set? Default: `TRUE`
#' @param es_classifier Optional: character. Vector of classifier names (and corresponding records) to retain in final result.
#' @param dsn Path to SQLite data source, or a `DBIConnection` to database with NASIS schema.
#'
#' @seealso [get_extended_data_from_NASIS_db()]
#'
#' @return a `data.frame`, or `NULL` on error
#' @export
get_ecosite_history_from_NASIS_db <- function(best = TRUE, SS = TRUE, dsn = NULL) {
get_ecosite_history_from_NASIS_db <- function(best = TRUE, SS = TRUE, es_classifier = NULL, dsn = NULL) {

.SD <- NULL

Expand Down Expand Up @@ -39,5 +40,5 @@ get_ecosite_history_from_NASIS_db <- function(best = TRUE, SS = TRUE, dsn = NULL
}

# load "best" siteecositehistory records: creates column 'es_selection_method' w/ "most recent" or "least missing data"
as.data.frame(data.table::as.data.table(ecositehistory)[, .pickBestEcosite(.SD), by = list(siteiid = ecositehistory$siteiid)])
as.data.frame(data.table::as.data.table(ecositehistory)[, .pickBestEcosite(.SD, es_classifier = es_classifier), by = list(siteiid = ecositehistory$siteiid)])
}
8 changes: 6 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,17 @@

## TODO: this may need some review
## try and pick the best possible ecosite record
.pickBestEcosite <- function(d) {
.pickBestEcosite <- function(d, es_classifier = NULL) {

if (!is.null(es_classifier)) {
d <- d[which(d$es_classifier %in% es_classifier),]
}

# add a method field
d$es_selection_method <- NA_character_

# try to get the most recent:
d.order <- order(d$ecositecorrdate, decreasing=TRUE)
d.order <- order(d$ecositecorrdate, decreasing = TRUE)

# if there are multiple (unique) dates, return the most recent
if (length(unique(d$ecositecorrdate)) > 1) {
Expand Down
9 changes: 8 additions & 1 deletion man/get_ecosite_history_from_NASIS_db.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit be15aba

Please sign in to comment.