From 965f72760eaa64f58144402db03d07b4e80b0c84 Mon Sep 17 00:00:00 2001 From: Sanne Govaert <44606923+sannegovaert@users.noreply.github.com> Date: Fri, 13 Dec 2024 10:45:21 +0100 Subject: [PATCH 1/8] Create individuals.R --- R/individuals.R | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 R/individuals.R diff --git a/R/individuals.R b/R/individuals.R new file mode 100644 index 00000000..10c91485 --- /dev/null +++ b/R/individuals.R @@ -0,0 +1,28 @@ +#' Get locations +#' +#' Gets the (unique) individuals from the observations of a Camera Trap Data +#' Package object. +#' +#' @inheritParams print.camtrapdp +#' @return A [tibble::tibble()] data frame with the individuals that have an +#' `individualID`, containing the following columns: +#' - `individualID` +#' - `scientificName` +#' - `lifeStage` +#' - `sex` +#' @family accessor functions +#' @export +#' @examples +#' x <- example_dataset() +#' individuals(x) +individuals <- function(x) { + check_camtrapdp(x) + observations(x) %>% + dplyr::distinct( + .data$individualID, + .data$scientificName, + .data$lifeStage, + .data$sex, + ) %>% + dplyr::filter(!is.na(individualID)) +} From d3f1cb35da8871f7df6727cc3c5e23ebe28edffc Mon Sep 17 00:00:00 2001 From: Sanne Govaert <44606923+sannegovaert@users.noreply.github.com> Date: Fri, 13 Dec 2024 10:45:39 +0100 Subject: [PATCH 2/8] document() --- NAMESPACE | 1 + man/deployments.Rd | 1 + man/events.Rd | 1 + man/individuals.Rd | 39 +++++++++++++++++++++++++++++++++++++++ man/locations.Rd | 1 + man/media.Rd | 1 + man/observations.Rd | 1 + man/taxa.Rd | 1 + 8 files changed, 46 insertions(+) create mode 100644 man/individuals.Rd diff --git a/NAMESPACE b/NAMESPACE index a7d40d3d..bd21bae0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,6 +12,7 @@ export(example_dataset) export(filter_deployments) export(filter_media) export(filter_observations) +export(individuals) export(locations) export(media) export(merge_camtrapdp) diff --git a/man/deployments.Rd b/man/deployments.Rd index 281ed9de..50fa5654 100644 --- a/man/deployments.Rd +++ b/man/deployments.Rd @@ -39,6 +39,7 @@ deployments(x) <- head(deployments(x), 1) \seealso{ Other accessor functions: \code{\link{events}()}, +\code{\link{individuals}()}, \code{\link{locations}()}, \code{\link{media}()}, \code{\link{observations}()}, diff --git a/man/events.Rd b/man/events.Rd index ee2245a6..b9a0e72f 100644 --- a/man/events.Rd +++ b/man/events.Rd @@ -31,6 +31,7 @@ events(x) \seealso{ Other accessor functions: \code{\link{deployments}()}, +\code{\link{individuals}()}, \code{\link{locations}()}, \code{\link{media}()}, \code{\link{observations}()}, diff --git a/man/individuals.Rd b/man/individuals.Rd new file mode 100644 index 00000000..6c7ad5f3 --- /dev/null +++ b/man/individuals.Rd @@ -0,0 +1,39 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/individuals.R +\name{individuals} +\alias{individuals} +\title{Get locations} +\usage{ +individuals(x) +} +\arguments{ +\item{x}{Camera Trap Data Package object, as returned by \code{\link[=read_camtrapdp]{read_camtrapdp()}}.} +} +\value{ +A \code{\link[tibble:tibble]{tibble::tibble()}} data frame with the individuals that have an +\code{individualID}, containing the following columns: +\itemize{ +\item \code{individualID} +\item \code{scientificName} +\item \code{lifeStage} +\item \code{sex} +} +} +\description{ +Gets the (unique) individuals from the observations of a Camera Trap Data +Package object. +} +\examples{ +x <- example_dataset() +individuals(x) +} +\seealso{ +Other accessor functions: +\code{\link{deployments}()}, +\code{\link{events}()}, +\code{\link{locations}()}, +\code{\link{media}()}, +\code{\link{observations}()}, +\code{\link{taxa}()} +} +\concept{accessor functions} diff --git a/man/locations.Rd b/man/locations.Rd index 20f4835a..ae5d40d1 100644 --- a/man/locations.Rd +++ b/man/locations.Rd @@ -32,6 +32,7 @@ locations(x) Other accessor functions: \code{\link{deployments}()}, \code{\link{events}()}, +\code{\link{individuals}()}, \code{\link{media}()}, \code{\link{observations}()}, \code{\link{taxa}()} diff --git a/man/media.Rd b/man/media.Rd index 5da31be5..fe5c794d 100644 --- a/man/media.Rd +++ b/man/media.Rd @@ -38,6 +38,7 @@ media(x) <- head(media(x), 1) Other accessor functions: \code{\link{deployments}()}, \code{\link{events}()}, +\code{\link{individuals}()}, \code{\link{locations}()}, \code{\link{observations}()}, \code{\link{taxa}()} diff --git a/man/observations.Rd b/man/observations.Rd index eb969eed..71875547 100644 --- a/man/observations.Rd +++ b/man/observations.Rd @@ -40,6 +40,7 @@ observations(x) <- head(observations(x), 1) Other accessor functions: \code{\link{deployments}()}, \code{\link{events}()}, +\code{\link{individuals}()}, \code{\link{locations}()}, \code{\link{media}()}, \code{\link{taxa}()} diff --git a/man/taxa.Rd b/man/taxa.Rd index c0f2e1b1..610fd42b 100644 --- a/man/taxa.Rd +++ b/man/taxa.Rd @@ -28,6 +28,7 @@ taxa(x) Other accessor functions: \code{\link{deployments}()}, \code{\link{events}()}, +\code{\link{individuals}()}, \code{\link{locations}()}, \code{\link{media}()}, \code{\link{observations}()} From f45331ddd531547825519d45f960921a99f04826 Mon Sep 17 00:00:00 2001 From: Sanne Govaert <44606923+sannegovaert@users.noreply.github.com> Date: Fri, 13 Dec 2024 10:55:10 +0100 Subject: [PATCH 3/8] Create test-individuals.R --- tests/testthat/test-individuals.R | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/testthat/test-individuals.R diff --git a/tests/testthat/test-individuals.R b/tests/testthat/test-individuals.R new file mode 100644 index 00000000..40feceb4 --- /dev/null +++ b/tests/testthat/test-individuals.R @@ -0,0 +1,31 @@ +test_that("individuals() returns a tibble", { + skip_if_offline() + x <- example_dataset() + expect_s3_class(individuals(x), "tbl") +}) + +test_that("individuals() returns the expected columns", { + skip_if_offline() + x <- example_dataset() + expected_cols <- c( + "individualID", + "scientificName", + "lifeStage", + "sex" + ) + expect_named(individuals(x), expected_cols) +}) + +test_that("individuals() returns the expected rows (unique individuals)", { + skip_if_offline() + x <- example_dataset() + + # Expect 0 individuals + expect_equal(nrow(individuals(x)), 0) + + # Expect 3 individuals + x$data$observations[1,]$individualID <- "anas_1" + x$data$observations[23,]$individualID <- "rattus_1" + x$data$observations[75,]$individualID <- "anas_2" + expect_equal(nrow(individuals(x)), 3) +}) From 72270c90f83697c665d9dc7c940de2f078bad4d0 Mon Sep 17 00:00:00 2001 From: Sanne Govaert <44606923+sannegovaert@users.noreply.github.com> Date: Fri, 13 Dec 2024 11:04:00 +0100 Subject: [PATCH 4/8] Update NEWS.md --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 5afc36f6..74999a51 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,7 @@ * New function `write_eml()` transforms Camtrap DP metadata to EML (#99). * New function `round_coordinates()` allows to fuzzy/generalize location information by rounding deployment `latitude` and `longitude`. It also updates `coordinateUncertainty` in the deployments and `coordinatePrecision` and spatial scope in the metadata (#106). * New function `shift_time()` allows to shift/correct date-times in data and metadata for specified deploymentIDs and duration (#108). +* New function `individuals()` returns a data frame with unique individuals (#149). * `filter_deployments()` and `deployments()<-` now update the spatial, temporal and taxonomic scope in the metadata based on the returned data (#100, #132). * `filter_observations()`, `filter_media()`, `media()<-` and `observations()<-` now update the taxonomic scope in the metadata based on the returned data (#89, #100, #130). * `read_camtrapdp()` now updates the spatial and temporal scope in metadata based on the data (#130). From 8b0888353fac681ed7b8c35a1f7a4736de83b8b3 Mon Sep 17 00:00:00 2001 From: Peter Desmet Date: Tue, 17 Dec 2024 13:40:14 +0100 Subject: [PATCH 5/8] Correct title --- R/individuals.R | 2 +- man/individuals.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/individuals.R b/R/individuals.R index 10c91485..cbd97f69 100644 --- a/R/individuals.R +++ b/R/individuals.R @@ -1,4 +1,4 @@ -#' Get locations +#' Get individuals #' #' Gets the (unique) individuals from the observations of a Camera Trap Data #' Package object. diff --git a/man/individuals.Rd b/man/individuals.Rd index 6c7ad5f3..5591a9e3 100644 --- a/man/individuals.Rd +++ b/man/individuals.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/individuals.R \name{individuals} \alias{individuals} -\title{Get locations} +\title{Get individuals} \usage{ individuals(x) } From 562fdf68dffb8ef90c615a07a24caf57afbec6fa Mon Sep 17 00:00:00 2001 From: Peter Desmet Date: Tue, 17 Dec 2024 13:40:22 +0100 Subject: [PATCH 6/8] Filter then distinct --- R/individuals.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/individuals.R b/R/individuals.R index cbd97f69..853b805b 100644 --- a/R/individuals.R +++ b/R/individuals.R @@ -18,11 +18,11 @@ individuals <- function(x) { check_camtrapdp(x) observations(x) %>% + dplyr::filter(!is.na(individualID)) %>% dplyr::distinct( .data$individualID, .data$scientificName, .data$lifeStage, - .data$sex, - ) %>% - dplyr::filter(!is.na(individualID)) + .data$sex + ) } From 76244b8252c1b79d073f6d1206c764e387ff4c75 Mon Sep 17 00:00:00 2001 From: Peter Desmet Date: Tue, 17 Dec 2024 13:42:14 +0100 Subject: [PATCH 7/8] Name individuals within a single event --- tests/testthat/test-individuals.R | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test-individuals.R b/tests/testthat/test-individuals.R index 40feceb4..81f20c18 100644 --- a/tests/testthat/test-individuals.R +++ b/tests/testthat/test-individuals.R @@ -23,9 +23,10 @@ test_that("individuals() returns the expected rows (unique individuals)", { # Expect 0 individuals expect_equal(nrow(individuals(x)), 0) - # Expect 3 individuals - x$data$observations[1,]$individualID <- "anas_1" - x$data$observations[23,]$individualID <- "rattus_1" - x$data$observations[75,]$individualID <- "anas_2" - expect_equal(nrow(individuals(x)), 3) + # Add 3 individuals to event 79204343 + # TODO: remove once https://github.com/tdwg/camtrap-dp/issues/396 is released + x$data$observations[x$data$observations$observationID == "05230014",]$individualID <- "Fezzik" + x$data$observations[x$data$observations$observationID == "5ead5692",]$individualID <- "Buttercup" + x$data$observations[x$data$observations$observationID == "29939500",]$individualID <- "Westley" + expect_length(individuals(x), 3) }) From 2600a679547e1b7d375eb94e25b8c215aefac03d Mon Sep 17 00:00:00 2001 From: Peter Desmet Date: Tue, 17 Dec 2024 13:50:36 +0100 Subject: [PATCH 8/8] Revert expect_ function --- tests/testthat/test-individuals.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-individuals.R b/tests/testthat/test-individuals.R index 81f20c18..a35f3255 100644 --- a/tests/testthat/test-individuals.R +++ b/tests/testthat/test-individuals.R @@ -28,5 +28,5 @@ test_that("individuals() returns the expected rows (unique individuals)", { x$data$observations[x$data$observations$observationID == "05230014",]$individualID <- "Fezzik" x$data$observations[x$data$observations$observationID == "5ead5692",]$individualID <- "Buttercup" x$data$observations[x$data$observations$observationID == "29939500",]$individualID <- "Westley" - expect_length(individuals(x), 3) + expect_equal(nrow(individuals(x)), 3) })