From 251e926a965e862250fd1a0a6efa09a77739c0a3 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 19 Mar 2024 11:41:27 +0100 Subject: [PATCH 01/33] create empty tests for `build_taxonomy()` functionality --- tests/testthat/test-build_taxonomy.R | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 tests/testthat/test-build_taxonomy.R diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R new file mode 100644 index 00000000..45626631 --- /dev/null +++ b/tests/testthat/test-build_taxonomy.R @@ -0,0 +1,7 @@ +test_that("build_taxonomy() returns tibble", { + +}) + +test_that("build_taxonomy() returns one row per species in $data$observations",{ + +}) From 01912026b0d8c1927a5134a7de862a1517d8c6d3 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 19 Mar 2024 12:59:55 +0100 Subject: [PATCH 02/33] add tests in line with `camtraptor::get_species()` --- tests/testthat/test-build_taxonomy.R | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index 45626631..03bc8594 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -3,5 +3,18 @@ test_that("build_taxonomy() returns tibble", { }) test_that("build_taxonomy() returns one row per species in $data$observations",{ + number_of_species <- + length(unique(example_package()$data$observations$scientificName)) +}) + +test_that("build_taxonomy() can handle missing vernacular names",{ + +}) + +test_that("build_taxonomy() returns NULL when there is no taxonomic information", { + +}) + +test_that("build_taxonomy() fills missing values with NA when a taxonomic field is only present for some of the records", { }) From df3ac7088fdf10e94a67120360a573467d10b1f7 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 19 Mar 2024 13:14:39 +0100 Subject: [PATCH 03/33] expect a data.frame as ouput --- tests/testthat/test-build_taxonomy.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index 03bc8594..62aa925b 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -1,5 +1,6 @@ test_that("build_taxonomy() returns tibble", { - + expect_s3_class(build_taxonomy(example_data()), + "data.frame") }) test_that("build_taxonomy() returns one row per species in $data$observations",{ From 2cc0e391023a167e9926ae02400a3ad602697151 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 19 Mar 2024 13:15:15 +0100 Subject: [PATCH 04/33] fix typo --- tests/testthat/test-build_taxonomy.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index 62aa925b..7fbc0964 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -1,11 +1,11 @@ test_that("build_taxonomy() returns tibble", { - expect_s3_class(build_taxonomy(example_data()), + expect_s3_class(build_taxonomy(example_dataset()), "data.frame") }) test_that("build_taxonomy() returns one row per species in $data$observations",{ number_of_species <- - length(unique(example_package()$data$observations$scientificName)) + length(unique(example_dataset()$data$observations$scientificName)) }) test_that("build_taxonomy() can handle missing vernacular names",{ From 99d02aa161ce70860b1ecf31e4ca0f9e83ab4157 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 19 Mar 2024 13:18:15 +0100 Subject: [PATCH 05/33] extract taxonomic information --- R/build_taxonomy.R | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 R/build_taxonomy.R diff --git a/R/build_taxonomy.R b/R/build_taxonomy.R new file mode 100644 index 00000000..3a5cbc73 --- /dev/null +++ b/R/build_taxonomy.R @@ -0,0 +1,14 @@ +#' Parse the taxon information from a Camera Trap Data Package object +#' +#' @param x +#' +#' @return A tibble with the taxonomic information from a Camera Trap Data +#' Package +#' +#' @examples +build_taxonomy <- function(x) { + # Extract the taxonomic information only + taxonomic_list <- x$taxonomic + + +} From 7c2c69a42bce3aeab7e30569962ee941a59c7a81 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 19 Mar 2024 13:18:22 +0100 Subject: [PATCH 06/33] parse into tibble --- R/build_taxonomy.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/build_taxonomy.R b/R/build_taxonomy.R index 3a5cbc73..ab9126d5 100644 --- a/R/build_taxonomy.R +++ b/R/build_taxonomy.R @@ -10,5 +10,8 @@ build_taxonomy <- function(x) { # Extract the taxonomic information only taxonomic_list <- x$taxonomic + # Convert list into a tibble with list columns + purrr::map(taxonomic_list, tibble::as_tibble) %>% + purrr::list_rbind() } From 24d0f6f979209730b3ca71bd278fec5ffa22d871 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 19 Mar 2024 13:19:35 +0100 Subject: [PATCH 07/33] Import `purrr` for list handling --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 89bcb09f..8b1770fc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,7 +19,8 @@ URL: https://github.com/inbo/camtrapdp, https://inbo.github.io/camtrapdp/ BugReports: https://github.com/inbo/camtrapdp/issues Imports: cli, - frictionless + frictionless, + purrr Suggests: testthat (>= 3.0.0) Encoding: UTF-8 From b08246fd752dcad62b8db51bdbaaa53d03d00841 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 19 Mar 2024 13:36:22 +0100 Subject: [PATCH 08/33] convert into data.frame instead, skip list columns --- R/build_taxonomy.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/R/build_taxonomy.R b/R/build_taxonomy.R index ab9126d5..75b5bf05 100644 --- a/R/build_taxonomy.R +++ b/R/build_taxonomy.R @@ -10,8 +10,7 @@ build_taxonomy <- function(x) { # Extract the taxonomic information only taxonomic_list <- x$taxonomic - # Convert list into a tibble with list columns - purrr::map(taxonomic_list, tibble::as_tibble) %>% + # Convert list into a data.frame + purrr::map(taxonomic_list, as.data.frame) %>% purrr::list_rbind() - } From 1ec63c7bcdd61b74e90cfae2b0de7a4b986f9492 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 19 Mar 2024 13:43:15 +0100 Subject: [PATCH 09/33] count species using dplyr so `na.rm` is explicit --- tests/testthat/test-build_taxonomy.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index 7fbc0964..be2bc09c 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -5,7 +5,10 @@ test_that("build_taxonomy() returns tibble", { test_that("build_taxonomy() returns one row per species in $data$observations",{ number_of_species <- - length(unique(example_dataset()$data$observations$scientificName)) + dplyr::n_distinct( + example_dataset()$data$observations$scientificName, + na.rm = TRUE) + }) test_that("build_taxonomy() can handle missing vernacular names",{ From 6a1fab363367c5236d7bcfd18c384083711a7dfc Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 19 Mar 2024 13:43:26 +0100 Subject: [PATCH 10/33] expect equal number of rows as number of species --- tests/testthat/test-build_taxonomy.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index be2bc09c..51f945c3 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -9,6 +9,10 @@ test_that("build_taxonomy() returns one row per species in $data$observations",{ example_dataset()$data$observations$scientificName, na.rm = TRUE) + expect_identical( + nrow(build_taxonomy(example_dataset())), + number_of_species + ) }) test_that("build_taxonomy() can handle missing vernacular names",{ From b3237c9652ccdff5316ceb89b61082adb930bb73 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 19 Mar 2024 13:44:33 +0100 Subject: [PATCH 11/33] create test skeleton for multiple languages in vernacularNames --- tests/testthat/test-build_taxonomy.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index 51f945c3..e2ecff62 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -26,3 +26,7 @@ test_that("build_taxonomy() returns NULL when there is no taxonomic information" test_that("build_taxonomy() fills missing values with NA when a taxonomic field is only present for some of the records", { }) + +test_that("build_taxonomy() creates a column per language for vernacularName", { + +}) From 3990dfdba8998152368e311bf37ac1122510fec1 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 19 Mar 2024 13:48:14 +0100 Subject: [PATCH 12/33] depend on `dplyr` for data wrangling (+ tidyselect) --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 8b1770fc..900214b0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,6 +19,7 @@ URL: https://github.com/inbo/camtrapdp, https://inbo.github.io/camtrapdp/ BugReports: https://github.com/inbo/camtrapdp/issues Imports: cli, + dplyr, frictionless, purrr Suggests: From abba42449d3d65b76985c4a7eecd217ecf89b674 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 19 Mar 2024 14:00:54 +0100 Subject: [PATCH 13/33] use `purrr::pluck` to quietly return NULL when `x$taxonomic` is missing --- R/build_taxonomy.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/build_taxonomy.R b/R/build_taxonomy.R index 75b5bf05..4291b8bb 100644 --- a/R/build_taxonomy.R +++ b/R/build_taxonomy.R @@ -8,7 +8,10 @@ #' @examples build_taxonomy <- function(x) { # Extract the taxonomic information only - taxonomic_list <- x$taxonomic + taxonomic_list <- purrr::pluck(x,"taxonomic") + + # If there is no taxonomic information, return NULL + if(is.null(taxonomic_list)){return(NULL)} # Convert list into a data.frame purrr::map(taxonomic_list, as.data.frame) %>% From 7a2b7b875ff1f25b3ad0f20559d1dad3edea7e11 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 09:20:56 +0100 Subject: [PATCH 14/33] test that `build_taxonomy()` is returning the right columns --- tests/testthat/test-build_taxonomy.R | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index e2ecff62..5c9ad947 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -15,6 +15,14 @@ test_that("build_taxonomy() returns one row per species in $data$observations",{ ) }) +test_that("build_taxonomy() returns the expected columns", { + expect_named( + build_taxonomy(example_dataset()), + c("scientificName", "taxonID", "taxonRank", "vernacularNames.eng", + "vernacularNames.nld") + ) +}) + test_that("build_taxonomy() can handle missing vernacular names",{ }) From b6380b7898d047e54dfc63bc131bd1516dc5ba5f Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 09:21:21 +0100 Subject: [PATCH 15/33] create Camera Trap Data Package object with missing vernacularName --- tests/testthat/test-build_taxonomy.R | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index 5c9ad947..9c68de72 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -24,6 +24,26 @@ test_that("build_taxonomy() returns the expected columns", { }) test_that("build_taxonomy() can handle missing vernacular names",{ + # Create a list with the taxonomic part of a Camera Trap Data Package object, + # the English vernacularName of Anas strepera is not provided. + missing_vernacular_name <- + list(taxonomic = list( + list( + scientificName = "Anas platyrhynchos", + taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGP6", + taxonRank = "species", + vernacularNames = list( + eng = "mallard", + nld = "wilde eend" + ) + ), + list( + scientificName = "Anas strepera", + taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGPL", + taxonRank = "species", + vernacularNames = list(nld = "krakeend") + ) + )) }) From 9e4b90166842b7562c12f56a4f9f9ee3c20eb02a Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 09:21:34 +0100 Subject: [PATCH 16/33] cover bug that results in extra columns --- tests/testthat/test-build_taxonomy.R | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index 9c68de72..bfcfe3f9 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -45,6 +45,13 @@ test_that("build_taxonomy() can handle missing vernacular names",{ ) )) + # Test to check that vernacularNames are still receiving the + # `vernacularNames.` prefix + expect_named( + build_taxonomy(missing_vernacular_name), + c("scientificName", "taxonID", "taxonRank", "vernacularNames.eng", + "vernacularNames.nld") + ) }) test_that("build_taxonomy() returns NULL when there is no taxonomic information", { From 95799e0a8f632f787cbd11db593f077567908c6a Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 09:24:10 +0100 Subject: [PATCH 17/33] skip tests that use `example_dataset()` when offline --- tests/testthat/test-build_taxonomy.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index bfcfe3f9..1653445c 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -1,9 +1,11 @@ test_that("build_taxonomy() returns tibble", { + skip_if_offline() expect_s3_class(build_taxonomy(example_dataset()), "data.frame") }) -test_that("build_taxonomy() returns one row per species in $data$observations",{ +test_that("build_taxonomy() returns one row per species in $data$observations" + skip_if_offline() number_of_species <- dplyr::n_distinct( example_dataset()$data$observations$scientificName, @@ -16,6 +18,7 @@ test_that("build_taxonomy() returns one row per species in $data$observations",{ }) test_that("build_taxonomy() returns the expected columns", { + skip_if_offline() expect_named( build_taxonomy(example_dataset()), c("scientificName", "taxonID", "taxonRank", "vernacularNames.eng", From 9135f3aacf7c59b2c5074cda59295267ca5739bf Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 09:39:39 +0100 Subject: [PATCH 18/33] move tests about, test for NULL when wanted --- tests/testthat/test-build_taxonomy.R | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index 1653445c..69ff29f0 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -57,14 +57,20 @@ test_that("build_taxonomy() can handle missing vernacular names",{ ) }) -test_that("build_taxonomy() returns NULL when there is no taxonomic information", { +test_that("build_taxonomy() fills missing values with NA when a taxonomic field is only present for some of the records", { }) -test_that("build_taxonomy() fills missing values with NA when a taxonomic field is only present for some of the records", { +test_that("build_taxonomy() creates a column per language for vernacularName", { }) -test_that("build_taxonomy() creates a column per language for vernacularName", { +test_that("build_taxonomy() returns NULL when there is no taxonomic information", { + skip_if_offline() + no_taxonomic_information <- example_dataset() + no_taxonomic_information$taxonomic <- NULL + expect_null( + build_taxonomy(no_taxonomic_information) + ) }) From 63fa2caead5a6d69a922a0c1f25bd3bdf87c8b84 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 09:57:30 +0100 Subject: [PATCH 19/33] Test for the right amount of vernacularName columns --- tests/testthat/test-build_taxonomy.R | 36 +++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index 69ff29f0..71cd2d0e 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -62,7 +62,41 @@ test_that("build_taxonomy() fills missing values with NA when a taxonomic field }) test_that("build_taxonomy() creates a column per language for vernacularName", { - + many_languages <- + list(taxonomic = list( + list( + scientificName = "Anas platyrhynchos", + taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGP6", + taxonRank = "species", + vernacularNames = list( + eng = "mallard", + nld = "wilde eend", + est = "sinikael-part", + glv = "Laagh Voirrey", + wel = "Hwyaden Wyllt", + afr = "Groenkopeend" + ) + ) + )) + # Expect 6 vernacularName columns + expect_length( + dplyr::select( + build_taxonomy(many_languages), + dplyr::starts_with("vernacularNames.") + ), + 6 + ) + # Expect the right vernacularName columns + expect_named( + dplyr::select( + build_taxonomy(many_languages), + dplyr::starts_with("vernacularNames.") + ), + c( + "vernacularNames.eng", "vernacularNames.nld", "vernacularNames.est", + "vernacularNames.glv", "vernacularNames.wel", "vernacularNames.afr" + ) + ) }) test_that("build_taxonomy() returns NULL when there is no taxonomic information", { From f76f8684e0b4f5c61d629befbcca45b3f1552bcf Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 09:58:09 +0100 Subject: [PATCH 20/33] add missing `{` --- tests/testthat/test-build_taxonomy.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index 71cd2d0e..a2e7af59 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -4,7 +4,7 @@ test_that("build_taxonomy() returns tibble", { "data.frame") }) -test_that("build_taxonomy() returns one row per species in $data$observations" +test_that("build_taxonomy() returns one row per species in $data$observations",{ skip_if_offline() number_of_species <- dplyr::n_distinct( From a91b73fcb069d82a86b5ef1907d2288fce7335f8 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 10:09:43 +0100 Subject: [PATCH 21/33] Test for filling missing values with NA --- tests/testthat/test-build_taxonomy.R | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index a2e7af59..c6161cfb 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -58,7 +58,40 @@ test_that("build_taxonomy() can handle missing vernacular names",{ }) test_that("build_taxonomy() fills missing values with NA when a taxonomic field is only present for some of the records", { + two_missing_vernacular_names <- + list(taxonomic = list( + list( + scientificName = "Anas platyrhynchos", + taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGP6", + taxonRank = "species", + vernacularNames = list( + eng = "mallard" + ) + ), + list( + scientificName = "Anas strepera", + taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGPL", + taxonRank = "species", + vernacularNames = list(nld = "krakeend") + ) + )) + # Check that we still get the `vernacularNames.` prefix + expect_named( + build_taxonomy(two_missing_vernacular_names), + c("scientificName", "taxonID", "taxonRank", "vernacularNames.eng", + "vernacularNames.nld") + ) + # Check that the Dutch vernacular name column contains an NA + expect_contains( + dplyr::pull(build_taxonomy(two_missing_vernacular_names), vernacularNames.nld), + NA_character_ + ) + # Check that the English vernacular name column contains an NA + expect_contains( + dplyr::pull(build_taxonomy(two_missing_vernacular_names), vernacularNames.eng), + NA_character_ + ) }) test_that("build_taxonomy() creates a column per language for vernacularName", { From 64d1dea48e782a296f9620006f394c73849382d8 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 10:10:21 +0100 Subject: [PATCH 22/33] Stylr --- R/build_taxonomy.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/build_taxonomy.R b/R/build_taxonomy.R index 4291b8bb..b7c02346 100644 --- a/R/build_taxonomy.R +++ b/R/build_taxonomy.R @@ -8,10 +8,12 @@ #' @examples build_taxonomy <- function(x) { # Extract the taxonomic information only - taxonomic_list <- purrr::pluck(x,"taxonomic") + taxonomic_list <- purrr::pluck(x, "taxonomic") # If there is no taxonomic information, return NULL - if(is.null(taxonomic_list)){return(NULL)} + if (is.null(taxonomic_list)) { + return(NULL) + } # Convert list into a data.frame purrr::map(taxonomic_list, as.data.frame) %>% From 37229b8c7320aa8e345119bb8a62ef831b1b54d7 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 10:21:23 +0100 Subject: [PATCH 23/33] use list flattening to retain hierarchy --- R/build_taxonomy.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/build_taxonomy.R b/R/build_taxonomy.R index b7c02346..be7d256d 100644 --- a/R/build_taxonomy.R +++ b/R/build_taxonomy.R @@ -16,6 +16,8 @@ build_taxonomy <- function(x) { } # Convert list into a data.frame - purrr::map(taxonomic_list, as.data.frame) %>% + purrr::map(taxonomic_list, purrr::list_flatten, + name_spec = "{outer}.{inner}") %>% + purrr::map(as.data.frame) %>% purrr::list_rbind() } From 2878b7d37f18f63532a483e35978078dc7b11f2c Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 11:10:35 +0100 Subject: [PATCH 24/33] devtools::document() --- man/build_taxonomy.Rd | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 man/build_taxonomy.Rd diff --git a/man/build_taxonomy.Rd b/man/build_taxonomy.Rd new file mode 100644 index 00000000..71a10385 --- /dev/null +++ b/man/build_taxonomy.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/build_taxonomy.R +\name{build_taxonomy} +\alias{build_taxonomy} +\title{Parse the taxon information from a Camera Trap Data Package object} +\usage{ +build_taxonomy(x) +} +\arguments{ +\item{x}{} +} +\value{ +A tibble with the taxonomic information from a Camera Trap Data +Package +} +\description{ +Parse the taxon information from a Camera Trap Data Package object +} From 65cda3aa29b86e9b86035bd5d4914a592b92b64a Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 11:14:53 +0100 Subject: [PATCH 25/33] remove empty examples --- R/build_taxonomy.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/build_taxonomy.R b/R/build_taxonomy.R index be7d256d..822ce043 100644 --- a/R/build_taxonomy.R +++ b/R/build_taxonomy.R @@ -4,8 +4,6 @@ #' #' @return A tibble with the taxonomic information from a Camera Trap Data #' Package -#' -#' @examples build_taxonomy <- function(x) { # Extract the taxonomic information only taxonomic_list <- purrr::pluck(x, "taxonomic") From ed520c7d71f7959cbd3f4b5249212f101830ae67 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 11:14:59 +0100 Subject: [PATCH 26/33] document args --- R/build_taxonomy.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/build_taxonomy.R b/R/build_taxonomy.R index 822ce043..20ff9511 100644 --- a/R/build_taxonomy.R +++ b/R/build_taxonomy.R @@ -1,6 +1,6 @@ #' Parse the taxon information from a Camera Trap Data Package object #' -#' @param x +#' @param x Camera Trap Data Package object. #' #' @return A tibble with the taxonomic information from a Camera Trap Data #' Package From 6bb7ebf78676337a29b298a3a81d598284d07fbb Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 11:15:14 +0100 Subject: [PATCH 27/33] devtools::document() --- man/build_taxonomy.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/build_taxonomy.Rd b/man/build_taxonomy.Rd index 71a10385..34eadd6d 100644 --- a/man/build_taxonomy.Rd +++ b/man/build_taxonomy.Rd @@ -7,7 +7,7 @@ build_taxonomy(x) } \arguments{ -\item{x}{} +\item{x}{Camera Trap Data Package object.} } \value{ A tibble with the taxonomic information from a Camera Trap Data From 7de202867b356d730ad1d02e18e660cc0afe7121 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 11:20:18 +0100 Subject: [PATCH 28/33] don't create rd for helper --- R/build_taxonomy.R | 1 + man/build_taxonomy.Rd | 18 ------------------ 2 files changed, 1 insertion(+), 18 deletions(-) delete mode 100644 man/build_taxonomy.Rd diff --git a/R/build_taxonomy.R b/R/build_taxonomy.R index 20ff9511..f72b6471 100644 --- a/R/build_taxonomy.R +++ b/R/build_taxonomy.R @@ -4,6 +4,7 @@ #' #' @return A tibble with the taxonomic information from a Camera Trap Data #' Package +#' @noRd build_taxonomy <- function(x) { # Extract the taxonomic information only taxonomic_list <- purrr::pluck(x, "taxonomic") diff --git a/man/build_taxonomy.Rd b/man/build_taxonomy.Rd deleted file mode 100644 index 34eadd6d..00000000 --- a/man/build_taxonomy.Rd +++ /dev/null @@ -1,18 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/build_taxonomy.R -\name{build_taxonomy} -\alias{build_taxonomy} -\title{Parse the taxon information from a Camera Trap Data Package object} -\usage{ -build_taxonomy(x) -} -\arguments{ -\item{x}{Camera Trap Data Package object.} -} -\value{ -A tibble with the taxonomic information from a Camera Trap Data -Package -} -\description{ -Parse the taxon information from a Camera Trap Data Package object -} From 0ec3d84141a985ab81150f206c248969b83c1d23 Mon Sep 17 00:00:00 2001 From: Peter Desmet Date: Wed, 20 Mar 2024 11:37:44 +0100 Subject: [PATCH 29/33] Update function documentation --- R/build_taxonomy.R | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/R/build_taxonomy.R b/R/build_taxonomy.R index f72b6471..10e8f927 100644 --- a/R/build_taxonomy.R +++ b/R/build_taxonomy.R @@ -1,12 +1,13 @@ -#' Parse the taxon information from a Camera Trap Data Package object +#' Build a data frame with taxonomic information #' -#' @param x Camera Trap Data Package object. +#' Builds a data frame from the `taxonomy` property in a Camera Trap Data +#' Package object. #' -#' @return A tibble with the taxonomic information from a Camera Trap Data -#' Package +#' @inheritParams version +#' @return `tibble()` data frame with the taxonomic information. #' @noRd build_taxonomy <- function(x) { - # Extract the taxonomic information only + # Extract the taxonomic information taxonomic_list <- purrr::pluck(x, "taxonomic") # If there is no taxonomic information, return NULL @@ -15,8 +16,11 @@ build_taxonomy <- function(x) { } # Convert list into a data.frame - purrr::map(taxonomic_list, purrr::list_flatten, - name_spec = "{outer}.{inner}") %>% + purrr::map( + taxonomic_list, + purrr::list_flatten, + name_spec = "{outer}.{inner}" + ) %>% purrr::map(as.data.frame) %>% purrr::list_rbind() } From 82a67dd96153774acda71760b46bf5ab0c169c82 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Wed, 20 Mar 2024 11:42:27 +0100 Subject: [PATCH 30/33] document that the output is a data.frame, not a tibble --- R/build_taxonomy.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/build_taxonomy.R b/R/build_taxonomy.R index 10e8f927..739835f7 100644 --- a/R/build_taxonomy.R +++ b/R/build_taxonomy.R @@ -4,7 +4,7 @@ #' Package object. #' #' @inheritParams version -#' @return `tibble()` data frame with the taxonomic information. +#' @return `data.frame()` data frame with the taxonomic information. #' @noRd build_taxonomy <- function(x) { # Extract the taxonomic information From 6515a7facfc2a216c511219f504c8b489587ba23 Mon Sep 17 00:00:00 2001 From: Peter Desmet Date: Wed, 20 Mar 2024 11:59:50 +0100 Subject: [PATCH 31/33] Use x in tests, always assign fake taxonomy to a camtrapdp object --- tests/testthat/test-build_taxonomy.R | 135 ++++++++++++++------------- 1 file changed, 68 insertions(+), 67 deletions(-) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index c6161cfb..37247c2b 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -1,36 +1,36 @@ -test_that("build_taxonomy() returns tibble", { +test_that("build_taxonomy() returns a data frame", { skip_if_offline() - expect_s3_class(build_taxonomy(example_dataset()), - "data.frame") + x <- example_dataset() + expect_s3_class(build_taxonomy(x), "data.frame") }) -test_that("build_taxonomy() returns one row per species in $data$observations",{ +test_that("build_taxonomy() returns one row per species in $data$observations", { skip_if_offline() + x <- example_dataset() number_of_species <- - dplyr::n_distinct( - example_dataset()$data$observations$scientificName, - na.rm = TRUE) + dplyr::n_distinct(x$data$observations$scientificName, na.rm = TRUE) expect_identical( - nrow(build_taxonomy(example_dataset())), + nrow(build_taxonomy(x)), number_of_species ) }) test_that("build_taxonomy() returns the expected columns", { skip_if_offline() + x <- example_dataset() expect_named( - build_taxonomy(example_dataset()), + build_taxonomy(x), c("scientificName", "taxonID", "taxonRank", "vernacularNames.eng", "vernacularNames.nld") ) }) -test_that("build_taxonomy() can handle missing vernacular names",{ - # Create a list with the taxonomic part of a Camera Trap Data Package object, - # the English vernacularName of Anas strepera is not provided. - missing_vernacular_name <- - list(taxonomic = list( +test_that("build_taxonomy() can handle missing vernacular names", { + x <- example_dataset() + # Create a taxonomy where the English vernacularName of Anas strepera is not + # provided. + taxonomy_missing_vernacular <- list( list( scientificName = "Anas platyrhynchos", taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGP6", @@ -44,87 +44,90 @@ test_that("build_taxonomy() can handle missing vernacular names",{ scientificName = "Anas strepera", taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGPL", taxonRank = "species", - vernacularNames = list(nld = "krakeend") + vernacularNames = list( + nld = "krakeend" + ) ) - )) + ) + x$taxonomic <- taxonomy_missing_vernacular - # Test to check that vernacularNames are still receiving the - # `vernacularNames.` prefix + # Check that we still get the `vernacularNames.` prefix expect_named( - build_taxonomy(missing_vernacular_name), + build_taxonomy(x), c("scientificName", "taxonID", "taxonRank", "vernacularNames.eng", "vernacularNames.nld") ) }) -test_that("build_taxonomy() fills missing values with NA when a taxonomic field is only present for some of the records", { - two_missing_vernacular_names <- - list(taxonomic = list( - list( - scientificName = "Anas platyrhynchos", - taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGP6", - taxonRank = "species", - vernacularNames = list( - eng = "mallard" - ) - ), - list( - scientificName = "Anas strepera", - taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGPL", - taxonRank = "species", - vernacularNames = list(nld = "krakeend") +test_that("build_taxonomy() fills missing values with NA when a taxonomic field + is only present for some of the records", { + x <- example_dataset() + taxonomy_missing_vernaculars <- list( + list( + scientificName = "Anas platyrhynchos", + taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGP6", + taxonRank = "species", + vernacularNames = list( + eng = "mallard" + ) + ), + list( + scientificName = "Anas strepera", + taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGPL", + taxonRank = "species", + vernacularNames = list( + nld = "krakeend" ) - )) + ) + ) + x$taxonomic <- taxonomy_missing_vernaculars # Check that we still get the `vernacularNames.` prefix expect_named( - build_taxonomy(two_missing_vernacular_names), + build_taxonomy(x), c("scientificName", "taxonID", "taxonRank", "vernacularNames.eng", "vernacularNames.nld") ) # Check that the Dutch vernacular name column contains an NA expect_contains( - dplyr::pull(build_taxonomy(two_missing_vernacular_names), vernacularNames.nld), + dplyr::pull(build_taxonomy(x), vernacularNames.nld), NA_character_ ) # Check that the English vernacular name column contains an NA expect_contains( - dplyr::pull(build_taxonomy(two_missing_vernacular_names), vernacularNames.eng), + dplyr::pull(build_taxonomy(x), vernacularNames.eng), NA_character_ ) }) test_that("build_taxonomy() creates a column per language for vernacularName", { - many_languages <- - list(taxonomic = list( - list( - scientificName = "Anas platyrhynchos", - taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGP6", - taxonRank = "species", - vernacularNames = list( - eng = "mallard", - nld = "wilde eend", - est = "sinikael-part", - glv = "Laagh Voirrey", - wel = "Hwyaden Wyllt", - afr = "Groenkopeend" - ) + x <- example_dataset() + taxonomy_many_languages <- list( + list( + scientificName = "Anas platyrhynchos", + taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGP6", + taxonRank = "species", + vernacularNames = list( + eng = "mallard", + nld = "wilde eend", + est = "sinikael-part", + glv = "Laagh Voirrey", + wel = "Hwyaden Wyllt", + afr = "Groenkopeend" ) - )) + ) + ) + x$taxonomic <- taxonomy_many_languages + # Expect 6 vernacularName columns expect_length( - dplyr::select( - build_taxonomy(many_languages), - dplyr::starts_with("vernacularNames.") - ), + dplyr::select(build_taxonomy(x), dplyr::starts_with("vernacularNames.")), 6 ) + # Expect the right vernacularName columns expect_named( - dplyr::select( - build_taxonomy(many_languages), - dplyr::starts_with("vernacularNames.") - ), + dplyr::select(build_taxonomy(x), dplyr::starts_with("vernacularNames.")), c( "vernacularNames.eng", "vernacularNames.nld", "vernacularNames.est", "vernacularNames.glv", "vernacularNames.wel", "vernacularNames.afr" @@ -134,10 +137,8 @@ test_that("build_taxonomy() creates a column per language for vernacularName", { test_that("build_taxonomy() returns NULL when there is no taxonomic information", { skip_if_offline() - no_taxonomic_information <- example_dataset() - no_taxonomic_information$taxonomic <- NULL + x <- example_dataset() + x$taxonomic <- NULL - expect_null( - build_taxonomy(no_taxonomic_information) - ) + expect_null(build_taxonomy(x)) }) From 340163f896f7a364eeeafc0e900ddfadcedbf5f8 Mon Sep 17 00:00:00 2001 From: Peter Desmet Date: Wed, 20 Mar 2024 12:00:39 +0100 Subject: [PATCH 32/33] Order tests logically --- tests/testthat/test-build_taxonomy.R | 86 ++++++++++++++-------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/tests/testthat/test-build_taxonomy.R b/tests/testthat/test-build_taxonomy.R index 37247c2b..33f603e9 100644 --- a/tests/testthat/test-build_taxonomy.R +++ b/tests/testthat/test-build_taxonomy.R @@ -4,6 +4,14 @@ test_that("build_taxonomy() returns a data frame", { expect_s3_class(build_taxonomy(x), "data.frame") }) +test_that("build_taxonomy() returns NULL when there is no taxonomic information", { + skip_if_offline() + x <- example_dataset() + x$taxonomic <- NULL + + expect_null(build_taxonomy(x)) +}) + test_that("build_taxonomy() returns one row per species in $data$observations", { skip_if_offline() x <- example_dataset() @@ -26,6 +34,41 @@ test_that("build_taxonomy() returns the expected columns", { ) }) +test_that("build_taxonomy() creates a column per language for vernacularName", { + x <- example_dataset() + taxonomy_many_languages <- list( + list( + scientificName = "Anas platyrhynchos", + taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGP6", + taxonRank = "species", + vernacularNames = list( + eng = "mallard", + nld = "wilde eend", + est = "sinikael-part", + glv = "Laagh Voirrey", + wel = "Hwyaden Wyllt", + afr = "Groenkopeend" + ) + ) + ) + x$taxonomic <- taxonomy_many_languages + + # Expect 6 vernacularName columns + expect_length( + dplyr::select(build_taxonomy(x), dplyr::starts_with("vernacularNames.")), + 6 + ) + + # Expect the right vernacularName columns + expect_named( + dplyr::select(build_taxonomy(x), dplyr::starts_with("vernacularNames.")), + c( + "vernacularNames.eng", "vernacularNames.nld", "vernacularNames.est", + "vernacularNames.glv", "vernacularNames.wel", "vernacularNames.afr" + ) + ) +}) + test_that("build_taxonomy() can handle missing vernacular names", { x <- example_dataset() # Create a taxonomy where the English vernacularName of Anas strepera is not @@ -99,46 +142,3 @@ test_that("build_taxonomy() fills missing values with NA when a taxonomic field NA_character_ ) }) - -test_that("build_taxonomy() creates a column per language for vernacularName", { - x <- example_dataset() - taxonomy_many_languages <- list( - list( - scientificName = "Anas platyrhynchos", - taxonID = "https://www.checklistbank.org/dataset/COL2023/taxon/DGP6", - taxonRank = "species", - vernacularNames = list( - eng = "mallard", - nld = "wilde eend", - est = "sinikael-part", - glv = "Laagh Voirrey", - wel = "Hwyaden Wyllt", - afr = "Groenkopeend" - ) - ) - ) - x$taxonomic <- taxonomy_many_languages - - # Expect 6 vernacularName columns - expect_length( - dplyr::select(build_taxonomy(x), dplyr::starts_with("vernacularNames.")), - 6 - ) - - # Expect the right vernacularName columns - expect_named( - dplyr::select(build_taxonomy(x), dplyr::starts_with("vernacularNames.")), - c( - "vernacularNames.eng", "vernacularNames.nld", "vernacularNames.est", - "vernacularNames.glv", "vernacularNames.wel", "vernacularNames.afr" - ) - ) -}) - -test_that("build_taxonomy() returns NULL when there is no taxonomic information", { - skip_if_offline() - x <- example_dataset() - x$taxonomic <- NULL - - expect_null(build_taxonomy(x)) -}) From cccf8213b2566c13bd7cfa3da10c8ab6591cf23f Mon Sep 17 00:00:00 2001 From: Peter Desmet Date: Wed, 20 Mar 2024 12:01:31 +0100 Subject: [PATCH 33/33] Update return --- R/build_taxonomy.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/build_taxonomy.R b/R/build_taxonomy.R index 739835f7..836c852a 100644 --- a/R/build_taxonomy.R +++ b/R/build_taxonomy.R @@ -4,7 +4,7 @@ #' Package object. #' #' @inheritParams version -#' @return `data.frame()` data frame with the taxonomic information. +#' @return Data frame with the taxonomic information. #' @noRd build_taxonomy <- function(x) { # Extract the taxonomic information