diff --git a/DESCRIPTION b/DESCRIPTION index 9acc3a2f..605be06f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -46,4 +46,4 @@ Suggests: Config/testthat/edition: 3 Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.2 +RoxygenNote: 7.3.3 diff --git a/NEWS.md b/NEWS.md index 10d1c6a8..f31a0ac3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # camtrapdp (development version) -* `read_camtrapdp()` now upgrades datasets to Camtrap DP 1.0.2 (#183). +* `read_camtrapdp()` now upgrades datasets to Camtrap DP 1.0.2 (#183) and provides help for unsupported versions. * `write_camtrapdp()` now removes properties with `NA` values from `x$taxonomic` and `x$contributors` which caused `datapackage.json` to be invalid (#186). * `write_eml()`'s derived paragraph is now formatted as DocBook rather than HTML (#188). diff --git a/R/read_camtrapdp.R b/R/read_camtrapdp.R index 5224bc3e..81c3952c 100644 --- a/R/read_camtrapdp.R +++ b/R/read_camtrapdp.R @@ -74,11 +74,16 @@ read_camtrapdp <- function(file) { # Check version version <- version(package) supported_versions <- c("1.0", "1.0.1", "1.0.2") + camtrapdp_version <- utils::packageVersion("camtrapdp") if (!version %in% supported_versions) { cli::cli_abort( c( - "{.val {version}} is not a supported Camtrap DP version.", - "i" = "Supported version{?s}: {.val {supported_versions}}." + "Can't read a dataset that is referencing Camtrap DP version + {.val {version}}.", + "i" = "Supported Camtrap DP version{?s}: {.val {supported_versions}}.", + "i" = "You currently use {.pkg camtrapdp} {.val {camtrapdp_version}}. + Updating to the latest version with {.code + install.packages(\"camtrapdp\")} might resolve this error." ), class = "camtrapdp_error_unsupported_version" ) diff --git a/R/round_coordinates.R b/R/round_coordinates.R index 6c6f74ec..acab6ef7 100644 --- a/R/round_coordinates.R +++ b/R/round_coordinates.R @@ -105,7 +105,7 @@ round_coordinates <- function(x, digits) { original_digits <- deployments(x) %>% dplyr::mutate( - lat_digits = nchar(gsub("^\\d*.", "", .data$latitude)) + lat_digits = nchar(stringr::str_remove(.data$latitude, "^\\d*\\.")) ) %>% dplyr::summarize(max(.data$lat_digits)) %>% dplyr::pull() diff --git a/R/taxa.R b/R/taxa.R index 851a772d..cc93e5ea 100644 --- a/R/taxa.R +++ b/R/taxa.R @@ -22,7 +22,7 @@ taxa <- function(x) { dplyr::filter(!is.na(.data$scientificName)) %>% dplyr::select("scientificName", dplyr::starts_with("taxon.")) %>% dplyr::distinct() %>% - dplyr::rename_with(~ sub("^taxon.", "", .x)) %>% + dplyr::rename_with(~ stringr::str_remove(.x, "^taxon\\.")) %>% dplyr::arrange(.data$scientificName) # Remove duplicates without taxonID diff --git a/R/update_metadata.R b/R/update_metadata.R index 0ca70292..8f1525f9 100644 --- a/R/update_metadata.R +++ b/R/update_metadata.R @@ -95,7 +95,7 @@ update_taxonomic <- function(x) { vernacularNames <- current_row %>% dplyr::select(dplyr::starts_with("vernacularNames")) %>% - dplyr::rename_with(~ sub("^vernacularNames.", "", .x)) %>% + dplyr::rename_with(~ stringr::str_remove(.x, "^vernacularNames\\.")) %>% as.list() # Append to taxonomic list diff --git a/R/version.R b/R/version.R index ca3416fe..9afe4e1f 100644 --- a/R/version.R +++ b/R/version.R @@ -36,8 +36,8 @@ version <- function(x) { match <- grep(pattern, profile) if (length(match) > 0) { extracted_version <- regmatches(profile, regexpr(pattern, profile)) - extracted_version <- sub("camtrap-dp/", "", extracted_version, fixed = TRUE) - extracted_version <- sub("/", "", extracted_version, fixed = TRUE) + extracted_version <- stringr::str_remove(extracted_version, "camtrap-dp/") + extracted_version <- stringr::str_remove(extracted_version, "/") extracted_version } else { profile @@ -52,13 +52,14 @@ version <- function(x) { new <- value # Update profile - x$profile <- sub(old, new, x$profile, fixed = TRUE) + x$profile <- stringr::str_replace(x$profile, old, new) # Update resource schemas resource_names <- c("deployments", "media", "observations") x$resources <- purrr::map(x$resources, function(resource) { if (resource$name %in% resource_names) { - resource$schema <- sub(old, new, resource$schema, fixed = TRUE) + resource$schema <- + stringr::str_replace(resource$schema, old, new) } resource }) diff --git a/man/camtrapdp-package.Rd b/man/camtrapdp-package.Rd index 789b09f3..5b8ad08e 100644 --- a/man/camtrapdp-package.Rd +++ b/man/camtrapdp-package.Rd @@ -29,7 +29,7 @@ Authors: Other contributors: \itemize{ - \item Research Institute for Nature and Forest (INBO) (00j54wy13) [copyright holder] + \item Research Institute for Nature and Forest (INBO) (\href{https://ror.org/00j54wy13}{ROR}) [copyright holder] \item Research Foundation - Flanders (https://lifewatch.be) [funder] } diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index a5c15997..6264fc03 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -16,10 +16,10 @@ #' ) #' remove_uuid(to_clean) remove_uuid <- function(character, replacement = "RANDOM_UUID") { - gsub( - "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}", # nolint: line_length_linter - replacement, - character + stringr::str_replace_all( + character, + "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}", + replacement ) } @@ -59,7 +59,7 @@ expect_meta_match <- function(file, core = "occurrence.csv", ...) { csv_file_cols <- readr::read_csv(file, show_col_types = FALSE) %>% colnames() %>% - purrr::map_chr(~ sub("^[A-Za-z]+:", "", .x)) # Remove namespace like "dcterms:" + purrr::map_chr(~ stringr::str_remove(.x, "^[A-Za-z]+:")) # Remove namespace like "dcterms:" csv_file_fields <- dplyr::tibble(field = csv_file_cols) %>% dplyr::mutate(index = as.integer(rownames(.)) - 1, .before = field) # Add index diff --git a/tests/testthat/test-round_coordinates.R b/tests/testthat/test-round_coordinates.R index 878c4a67..ba659257 100644 --- a/tests/testthat/test-round_coordinates.R +++ b/tests/testthat/test-round_coordinates.R @@ -150,6 +150,10 @@ test_that("round_coordinates() updates spatial scope in metadata", { x1 <- round_coordinates(x, 1) # The number of digits in spatial scope is updated - expect_equal(max(nchar(gsub("^\\d*.", "", x2$spatial$coordinates))), 2) - expect_equal(max(nchar(gsub("^\\d*.", "", x1$spatial$coordinates))), 1) + expect_equal(max(stringr::str_length( + stringr::str_remove(x2$spatial$coordinates, "^\\d*\\.") + )), 2) + expect_equal(max(stringr::str_length( + stringr::str_remove(x1$spatial$coordinates, "^\\d*\\.") + )), 1) })