-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from mhpob/split-qmd-tag
Break template into self-contained functions
- Loading branch information
Showing
54 changed files
with
2,245 additions
and
921 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: otndo | ||
Title: Understand your OTN data | ||
Version: 0.1.10 | ||
Version: 0.2.0 | ||
Authors@R: | ||
person("Michael", "O'Brien", , "[email protected]", role = c("aut", "cre"), | ||
comment = c(ORCID = "0000-0003-1420-6395")) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
# otndo 0.1.02 | ||
## otndo 0.2 | ||
### v 0.2.0 | ||
|
||
* Fix issue where otndo would get lost when deployment metadata sheet wasn't labeled | ||
* Break out all of the functions internal to the QMD template. Will allow for clearer errors and more-directed testing. | ||
|
||
# otndo 0.1.0 | ||
## otndo 0.1 | ||
|
||
* Fix issue where otndo would get lost when deployment metadata sheet wasn't labeled | ||
* Switch to semantic versioning | ||
* Add figure and table captions | ||
* Runiverse! | ||
* Miscellaneous fixes | ||
* Combine the PIs/POCs of projects with changing staff | ||
|
||
# otndo 0.0.0.9000 | ||
## otndo 0.0.0.9000 | ||
|
||
* Added a `NEWS.md` file to track changes to the package. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#' Create a Gantt-like chart of receiver deployments and recoveries | ||
#' | ||
#' @param deployment Cleaned deployment metadata sheet(s). Assumes it was | ||
#' cleaned with the internal `otndo:::clean_otn_deployment` function, read in, | ||
#' and converted to a data.table. | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' # Download a deployment metadata file | ||
#' td <- file.path(tempdir(), "matos_test_files") | ||
#' dir.create(td) | ||
#' | ||
#' download.file( | ||
#' paste0( | ||
#' "https://members.oceantrack.org/data/repository/pbsm/", | ||
#' "data-and-metadata/2018/pbsm-instrument-deployment-short-form-2018.xls/", | ||
#' "@@download/file" | ||
#' ), | ||
#' destfile = file.path(td, "pbsm-instrument-deployment-short-form-2018.xls"), | ||
#' mode = "wb" | ||
#' ) | ||
#' | ||
#' # Use internal function to clean | ||
#' deployment_filepath <- otndo:::write_to_tempdir( | ||
#' type = "deployment", | ||
#' files = file.path(td, "pbsm-instrument-deployment-short-form-2018.xls"), | ||
#' temp_dir = td | ||
#' ) | ||
#' | ||
#' # Make the Gantt chart | ||
#' deployment_gantt( | ||
#' data.table::fread(deployment_filepath) | ||
#' ) | ||
#' } | ||
#' | ||
#' @export | ||
deployment_gantt <- function(deployment) { | ||
stationname <- deploy_date_time <- recover_date_time <- NULL | ||
|
||
ggplot2::ggplot(data = deployment) + | ||
ggplot2::geom_linerange( | ||
ggplot2::aes( | ||
y = stationname, | ||
xmin = deploy_date_time, | ||
xmax = recover_date_time | ||
), | ||
linewidth = 5 | ||
) + | ||
ggplot2::geom_linerange( | ||
ggplot2::aes( | ||
ymin = as.numeric(factor(stationname)) - 0.4, | ||
ymax = as.numeric(factor(stationname)) + 0.4, | ||
x = recover_date_time | ||
), | ||
color = "red", linewidth = 2 | ||
) + | ||
ggplot2::scale_x_datetime(date_breaks = "month", date_labels = "%b %y") + | ||
ggplot2::labs(x = NULL, y = NULL, title = "Temporal receiver coverage") + | ||
ggplot2::theme_minimal() + | ||
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#' Plot the geographic extent of OTN projects | ||
#' | ||
#' @param otn_tables A list containing OTN's `otn_resources_metadata_points` | ||
#' GeoServer layer. Usually created using `otn_query`. | ||
#' | ||
#' @examples | ||
#' match_map( | ||
#' otn_query("MDWEA") | ||
#' ) | ||
#' | ||
#' @export | ||
|
||
match_map <- function(otn_tables) { | ||
natural_earth <- sf::st_read( | ||
system.file("ne_110m_coastline.gpkg", | ||
package = "otndo" | ||
), | ||
quiet = T | ||
) | ||
|
||
otn_sf <- otn_tables$otn_resources_metadata_points |> | ||
sf::st_as_sf(wkt = "the_geom", crs = 4326) | ||
otn_limits <- sf::st_bbox(otn_sf) | ||
|
||
ggplot2::ggplot() + | ||
ggplot2::geom_sf(data = natural_earth) + | ||
ggplot2::geom_sf(data = otn_sf, fill = NA, color = "blue") + | ||
ggplot2::coord_sf( | ||
xlim = c(otn_limits["xmin"] - 5, otn_limits["xmax"] + 5), | ||
ylim = c(otn_limits["ymin"] - 5, otn_limits["ymax"] + 5) | ||
) + | ||
ggplot2::labs(title = "Geographic extent of detected projects") + | ||
ggplot2::theme_minimal() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
#' Create a reactable table of matched detections | ||
#' | ||
#' @param extract matched (transmitter) or qualified (receiver) OTN detections | ||
#' @param type Tag or receiver data? Takes values of "tag" and "receiver"; | ||
#' defaults to "tag". | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' # Receiver | ||
#' download.file( | ||
#' paste0( | ||
#' "https://members.oceantrack.org/data/repository/pbsm/", | ||
#' "detection-extracts/pbsm_qualified_detections_2018.zip/", | ||
#' "@@download/file" | ||
#' ), | ||
#' destfile = file.path(td, "pbsm_qualified_detections_2018.zip"), | ||
#' mode = "wb" | ||
#' ) | ||
#' unzip( | ||
#' file.path(td, "pbsm_qualified_detections_2018.zip"), | ||
#' exdir = td | ||
#' ) | ||
#' | ||
#' qualified_dets <- data.table::fread( | ||
#' file.path(td, "pbsm_qualified_detections_2018.csv") | ||
#' ) | ||
#' | ||
#' match_table( | ||
#' extract = qualified_dets, | ||
#' type = "receiver" | ||
#' ) | ||
#' | ||
#' # Transmitters | ||
#' download.file( | ||
#' paste0( | ||
#' "https://members.oceantrack.org/data/repository/", | ||
#' "pbsm/detection-extracts/pbsm_matched_detections_2018.zip/", | ||
#' "@@download/file" | ||
#' ), | ||
#' destfile = file.path(td, "pbsm_matched_detections_2018.zip"), | ||
#' mode = "wb" | ||
#' ) | ||
#' unzip(file.path(td, "pbsm_matched_detections_2018.zip"), | ||
#' exdir = td | ||
#' ) | ||
#' | ||
#' matched_dets <- data.table::fread( | ||
#' file.path(td, "pbsm_matched_detections_2018.csv") | ||
#' ) | ||
#' | ||
#' match_table( | ||
#' extract = matched_dets, | ||
#' type = "tag" | ||
#' ) | ||
#' } | ||
#' | ||
#' @export | ||
match_table <- function( | ||
extract, | ||
type = c("tag", "receiver")) { | ||
mt_data <- prep_match_table(extract, type) | ||
|
||
reactable::reactable( | ||
mt_data, | ||
columns = list( | ||
PI = reactable::colDef( | ||
html = T, | ||
cell = function(value, index) { | ||
sprintf( | ||
'<a href=mailto:%s target="_blank">%s</a>', | ||
mt_data$PI_emails[index], value | ||
) | ||
}, | ||
minWidth = 150 | ||
), | ||
POC = reactable::colDef( | ||
html = T, | ||
cell = function(value, index) { | ||
sprintf( | ||
'<a href=mailto:%s target="_blank">%s</a>', | ||
mt_data$POC_emails[index], value | ||
) | ||
}, | ||
minWidth = 150 | ||
), | ||
PI_emails = reactable::colDef(show = F), | ||
POC_emails = reactable::colDef(show = F), | ||
`Project name` = reactable::colDef(minWidth = 200) | ||
) | ||
) | ||
} | ||
|
||
|
||
#' Prepare the detection match summary data | ||
#' | ||
#' @inheritParams match_table | ||
prep_match_table <- function( | ||
extract, | ||
type = c("tag", "receiver")) { | ||
. <- collectioncode <- project_name <- resource_full_name <- PI <- POC <- | ||
network <- code <- detections <- individuals <- PI_emails <- POC_emails <- | ||
station <- Station <- Detections <- Individuals <- longitude <- latitude <- | ||
detectedby <- NULL | ||
|
||
extract <- data.table::data.table(extract) | ||
|
||
if (type == "tag") { | ||
mt <- merge( | ||
extract[, .(detections = .N), by = "detectedby"], | ||
unique(extract, by = c("tagname", "detectedby"))[ | ||
, .(individuals = .N), | ||
by = "detectedby" | ||
] | ||
) | ||
|
||
data.table::setnames(mt, "detectedby", "project_name") | ||
|
||
otn <- otn_query(unique(extract$detectedby)) | ||
} else { | ||
mt <- merge( | ||
extract[, .(detections = .N), by = "trackercode"], | ||
unique(extract, by = "fieldnumber")[, .(individuals = .N), | ||
by = "trackercode" | ||
] | ||
) | ||
|
||
data.table::setnames(mt, "trackercode", "project_name") | ||
|
||
otn <- otn_query(unique(extract$trackercode)) | ||
} | ||
|
||
pis <- project_contacts(extract, type = type) | ||
mt <- merge(mt, pis) | ||
|
||
mt[, collectioncode := gsub(".*\\.", "", project_name)] | ||
|
||
mt <- merge( | ||
mt, | ||
otn[[1]][ | ||
, | ||
.( | ||
resource_full_name, | ||
collectioncode | ||
) | ||
], | ||
by = "collectioncode" | ||
) | ||
|
||
|
||
mt[, ":="(network = gsub("\\..*", "", project_name), | ||
code = gsub(".*\\.", "", project_name), | ||
project_name = NULL, | ||
PI = data.table::fifelse(PI == "NA", "", PI), | ||
POC = data.table::fifelse(POC == "NA", "", POC))] | ||
mt[, network := data.table::fifelse(network == code, "", network)] | ||
|
||
mt <- mt[, .( | ||
PI, POC, resource_full_name, network, code, | ||
detections, individuals, PI_emails, POC_emails | ||
)] | ||
data.table::setnames(mt, c( | ||
"PI", "POC", "Project name", "Network", "Project code", | ||
"Detections", "Individuals", "PI_emails", "POC_emails" | ||
)) | ||
|
||
data.table::setorder(mt, -"Detections", -"Individuals") | ||
|
||
mt[] | ||
} |
Oops, something went wrong.