Skip to content

Commit be15aba

Browse files
committed
get_ecosite_history_from_NASIS_db: allow custom filtering on ES classifier field; thanks to @natearoe
1 parent 3c557a4 commit be15aba

3 files changed

+17
-5
lines changed

R/get_ecosite_history_from_NASIS_db.R

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
#'
55
#' @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.
66
#' @param SS Use selected set? Default: `TRUE`
7+
#' @param es_classifier Optional: character. Vector of classifier names (and corresponding records) to retain in final result.
78
#' @param dsn Path to SQLite data source, or a `DBIConnection` to database with NASIS schema.
89
#'
910
#' @seealso [get_extended_data_from_NASIS_db()]
1011
#'
1112
#' @return a `data.frame`, or `NULL` on error
1213
#' @export
13-
get_ecosite_history_from_NASIS_db <- function(best = TRUE, SS = TRUE, dsn = NULL) {
14+
get_ecosite_history_from_NASIS_db <- function(best = TRUE, SS = TRUE, es_classifier = NULL, dsn = NULL) {
1415

1516
.SD <- NULL
1617

@@ -39,5 +40,5 @@ get_ecosite_history_from_NASIS_db <- function(best = TRUE, SS = TRUE, dsn = NULL
3940
}
4041

4142
# load "best" siteecositehistory records: creates column 'es_selection_method' w/ "most recent" or "least missing data"
42-
as.data.frame(data.table::as.data.table(ecositehistory)[, .pickBestEcosite(.SD), by = list(siteiid = ecositehistory$siteiid)])
43+
as.data.frame(data.table::as.data.table(ecositehistory)[, .pickBestEcosite(.SD, es_classifier = es_classifier), by = list(siteiid = ecositehistory$siteiid)])
4344
}

R/utils.R

+6-2
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,17 @@
103103

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

108+
if (!is.null(es_classifier)) {
109+
d <- d[which(d$es_classifier %in% es_classifier),]
110+
}
111+
108112
# add a method field
109113
d$es_selection_method <- NA_character_
110114

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

114118
# if there are multiple (unique) dates, return the most recent
115119
if (length(unique(d$ecositecorrdate)) > 1) {

man/get_ecosite_history_from_NASIS_db.Rd

+8-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)