diff --git a/DESCRIPTION b/DESCRIPTION index 533b0a9a..8d4c3d81 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -46,4 +46,4 @@ Suggests: Config/testthat/edition: 3 Encoding: UTF-8 Roxygen: list(markdown = TRUE) -Config/roxygen2/version: 8.0.0 +Config/roxygen2/version: 8.1.0 diff --git a/R/downgrade_package.R b/R/downgrade_package.R new file mode 100644 index 00000000..f6c0890b --- /dev/null +++ b/R/downgrade_package.R @@ -0,0 +1,30 @@ +#' Downgrade a Camera Trap Data Package to Data Package v1 +#' +#' Packages, schemas and resources created with frictionless >=2.0.0 follow the +#' v2 specification of Data Package, while Camtrap DP follows v1. +#' This function downgrades a package and its resources to v1. +#' +#' @inheritParams print.camtrapdp +#' @returns Downgraded `x`. +#' @family helper functions +#' @noRd +downgrade_package <- function(x) { + # Downgrade descriptor + x$`$schema` <- NULL # Profile expected to be present + + # Downgrade resources + for (resource_name in frictionless::resource_names(x)) { + resource <- frictionless::resource(x, resource_name) + if (frictionless::version(resource) != "1.0") { + # Set profile (after name and description). Note: also removes attributes + resource <- append(resource, list(profile = "tabular-data-resource"), 2) + + # Remove $schema and type + resource$`$schema` <- NULL + resource$type <- NULL + frictionless::resource(x, resource_name) <- resource + } + } + + return(x) +} diff --git a/R/write_camtrapdp.R b/R/write_camtrapdp.R index 49cd1384..30a652bc 100644 --- a/R/write_camtrapdp.R +++ b/R/write_camtrapdp.R @@ -66,6 +66,9 @@ write_camtrapdp <- function(x, directory, ...) { recursive = TRUE ) + # Downgrade to Data Package v1 + x <- downgrade_package(x) + # Write files frictionless::write_package(x, directory, ...) diff --git a/tests/testthat/test-write_camtrapdp.R b/tests/testthat/test-write_camtrapdp.R index 3e61256f..b51a2a15 100644 --- a/tests/testthat/test-write_camtrapdp.R +++ b/tests/testthat/test-write_camtrapdp.R @@ -100,8 +100,9 @@ test_that("write_camtrapdp() returns the expected datapackage.json for the on.exit(unlink(temp_dir, recursive = TRUE)) # Adapt x$taxonomic and x$contributors to test for https://github.com/inbo/camtrapdp/issues/185 - x <- x |> + x <- suppressMessages( update_taxon( + x, from = "Anas platyrhynchos", to = list( scientificName = "Anas platyrhynchos", @@ -110,6 +111,7 @@ test_that("write_camtrapdp() returns the expected datapackage.json for the vernacularNames.nld = "wilde eend" # Assigns NA for vernacularNames.eng ) ) + ) contributors(x) <- contributors(x) # Assigns NA for e.g. firstName of INBO write_camtrapdp(x, temp_dir)