-
Notifications
You must be signed in to change notification settings - Fork 0
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 #28 from inbo/27-create-helper-build_taxonomy
Create helper `build_taxonomy()`
- Loading branch information
Showing
3 changed files
with
172 additions
and
1 deletion.
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
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,26 @@ | ||
#' Build a data frame with taxonomic information | ||
#' | ||
#' Builds a data frame from the `taxonomy` property in a Camera Trap Data | ||
#' Package object. | ||
#' | ||
#' @inheritParams version | ||
#' @return Data frame with the taxonomic information. | ||
#' @noRd | ||
build_taxonomy <- function(x) { | ||
# Extract the taxonomic information | ||
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, | ||
purrr::list_flatten, | ||
name_spec = "{outer}.{inner}" | ||
) %>% | ||
purrr::map(as.data.frame) %>% | ||
purrr::list_rbind() | ||
} |
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,144 @@ | ||
test_that("build_taxonomy() returns a data frame", { | ||
skip_if_offline() | ||
x <- example_dataset() | ||
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() | ||
number_of_species <- | ||
dplyr::n_distinct(x$data$observations$scientificName, na.rm = TRUE) | ||
|
||
expect_identical( | ||
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(x), | ||
c("scientificName", "taxonID", "taxonRank", "vernacularNames.eng", | ||
"vernacularNames.nld") | ||
) | ||
}) | ||
|
||
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 | ||
# provided. | ||
taxonomy_missing_vernacular <- 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" | ||
) | ||
) | ||
) | ||
x$taxonomic <- taxonomy_missing_vernacular | ||
|
||
# Check that we still get the `vernacularNames.` prefix | ||
expect_named( | ||
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", { | ||
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(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(x), vernacularNames.nld), | ||
NA_character_ | ||
) | ||
# Check that the English vernacular name column contains an NA | ||
expect_contains( | ||
dplyr::pull(build_taxonomy(x), vernacularNames.eng), | ||
NA_character_ | ||
) | ||
}) |