Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to explore parsing issues in resources #109

Merged
merged 7 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: camtraptor
Title: Read, Explore and Visualize Camera Trap Data Packages
Version: 0.13.0
Version: 0.14.0
Authors@R: c(
person(given = "Damiano",
family = "Oldoni",
Expand Down
56 changes: 48 additions & 8 deletions R/read_camtrap_dp.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
#' media file to speed up reading larger Camtrap DP packages.
#' @param path Path to the directory containing the datapackage. Use `file`
#' with path or URL to a `datapackage.json` file instead.
#' @return A list containing three (tibble) data.frames: 1. `observations` 2.
#' `deployments` 3. `media`
#' @return A list containing three (tibble) data.frames:
#' 1. `observations`
#' 2. `deployments`
#' 3. `media`
#'
#' and a list with metadata: `datapackage`
#' and a list with metadata: `datapackage`.
#'
#' @export
#'
Expand All @@ -33,6 +35,19 @@
#'
#' # Read Camtrap DP package and ignore media file
#' muskrat_coypu <- read_camtrap_dp(camtrap_dp_file, media = FALSE)
#'
#' # If parsing issues while reading deployments, observations or media arise,
#' use readr::problems()
#' camtrap_dp_file_with_issues <- system.file(
#' "extdata",
#' "mica_parsing_issues",
#' "datapackage_for_parsing_issues.json",
#' package = "camtraptor"
#' )
#' muskrat_coypu_with_issues <- read_camtrap_dp(camtrap_dp_file_with_issues, media = TRUE)
#' readr::problems(muskrat_coypu_with_issues$deployments)
#' readr::problems(muskrat_coypu_with_issues$observations)
#' readr::problems(muskrat_coypu_with_issues$media)
#' }
read_camtrap_dp <- function(file = NULL,
media = TRUE,
Expand Down Expand Up @@ -65,7 +80,23 @@ read_camtrap_dp <- function(file = NULL,
# read files
package <- frictionless::read_package(file)
deployments <- frictionless::read_resource(package, "deployments")
issues_deployments <- readr::problems(deployments)
if (nrow(issues_deployments) > 0) {
warning(
glue("One or more parsing issues occurred while reading deployments. ",
"On how to use readr::problems() with datapackages, ",
"see examples in documentation of function read_camtrap_dp."
))
}
observations <- frictionless::read_resource(package, "observations")
issues_observations <- readr::problems(observations)
if (nrow(issues_observations) > 0) {
warning(
glue("One or more parsing issues occurred while reading observations. ",
"On how to use readr::problems() with datapackages, ",
"see examples in documentation of function read_camtrap_dp."
))
}

# get taxonomic info
taxon_infos <- get_species(list(
Expand All @@ -82,25 +113,34 @@ read_camtrap_dp <- function(file = NULL,
by = c("taxonID", "scientificName"))
observations <- observations %>%
dplyr::relocate(dplyr::one_of(cols_taxon_infos), .after = .data$cameraSetup)
# Inherit parsing issues from reading
attr(observations, which = "problems") <- issues_observations
}
if (media == TRUE) {
media <- frictionless::read_resource(package, "media")
issues_media <- readr::problems(media)
if (nrow(issues_media) > 0) {
warning(glue("One or more parsing issues occurred while reading media. ",
"On how to use readr::problems() with datapackages, ",
"see examples in documentation of function read_camtrap_dp.")
)
}
}

# return list
if (is.data.frame(media)) {
list(
"datapackage" = package,
"deployments" = dplyr::tibble(deployments),
"media" = dplyr::tibble(media),
"observations" = dplyr::tibble(observations)
"deployments" = deployments,
"media" = media,
"observations" = observations
)
} else {
list(
"datapackage" = package,
"deployments" = dplyr::tibble(deployments),
"deployments" = deployments,
"media" = NULL,
"observations" = dplyr::tibble(observations)
"observations" = observations
)
}
}
Loading