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

Create contributors() #140

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export("deployments<-")
export("media<-")
export("observations<-")
export(check_camtrapdp)
export(contributors)
export(deployments)
export(events)
export(example_dataset)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* New function `write_eml()` transforms Camtrap DP metadata to EML (#99).
* New function `round_coordinates()` allows to fuzzy/generalize location information by rounding deployment `latitude` and `longitude`. It also updates `coordinateUncertainty` in the deployments and `coordinatePrecision` and spatial scope in the metadata (#106).
* New function `shift_time()` allows to shift/correct date-times in data and metadata for specified deploymentIDs and duration (#108).
* New accessor function `contributors()` returns a data frame with contributors (#140).
* `filter_deployments()` and `deployments()<-` now update the spatial, temporal and taxonomic scope in the metadata based on the returned data (#100, #132).
* `filter_observations()`, `filter_media()`, `media()<-` and `observations()<-` now update the taxonomic scope in the metadata based on the returned data (#89, #100, #130).
* `read_camtrapdp()` now updates the spatial and temporal scope in metadata based on the data (#130).
Expand Down
44 changes: 44 additions & 0 deletions R/contributors.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#' Get contributors and build a data frame
#'
#' Gets the `x$contributors` property in a Camera Trap Data Package object and
#' builds a data frame with the contributors.
#'
#' @inheritParams print.camtrapdp
#' @return A data frame with the contributors.
#' @family accessor functions
#' @export
#' @examples
#' x <- example_dataset()
#' contributors(x)
contributors <- function(x) {
orcid_regex <- "(\\d{4}-){3}\\d{3}(\\d|X)"
contributors <-
purrr::map_dfr(
x$contributors,
~ as.data.frame(., stringsAsFactors = FALSE)
) %>%
dplyr::filter(!.data$role %in% c("rightsHolder", "publisher")) %>%
mutate_when_missing(path = character()) %>% # Guarantee path col
dplyr::mutate(
first_name = purrr::map_chr(
.data$title,
~ strsplit(.x, " ", fixed = TRUE)[[1]][1] # First string before space
),
last_name = purrr::map_chr(
.data$title,
~ sub("^\\S* ", "", .x) # Remove string up until first space
),
orcid = ifelse( # Move ORCID from path to separate column
!is.na(regexpr(orcid_regex, .data$path)),
regmatches(.data$path, regexpr(orcid_regex, .data$path)),
NA_character_
),
path = ifelse(
grepl(orcid_regex, .data$path),
NA_character_,
.data$path
)
)
return(contributors)
}

5 changes: 3 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ expand_cols <- function(df, colnames) {

#' Creates list of contributors in EML format
#'
#' @param contributor_list List of contributors
#' @param contributors A data frame with the contributors
#' @return List of contributors as emld responsibleParty objects.
#' @family helper functions
#' @noRd
create_eml_contributors <- function(contributor_list) {
create_eml_contributors <- function(contributors) {
contributor_list <- purrr::transpose(contributors)
purrr::map(contributor_list, ~ EML::set_responsibleParty(
givenName = .$first_name,
surName = .$last_name,
Expand Down
40 changes: 4 additions & 36 deletions R/write_eml.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,47 +96,15 @@ write_eml <- function(x, directory, derived_paragraph = TRUE) {
)

# Convert contributors to a data frame
orcid_regex <- "(\\d{4}-){3}\\d{3}(\\d|X)"
creators <-
purrr::map_dfr(
x$contributors,
~ as.data.frame(., stringsAsFactors = FALSE)
) %>%
dplyr::filter(!.data$role %in% c("rightsHolder", "publisher")) %>%
mutate_when_missing(path = character()) %>% # Guarantee path col
dplyr::mutate(
first_name = purrr::map_chr(
.data$title,
~ strsplit(.x, " ", fixed = TRUE)[[1]][1] # First string before space
),
last_name = purrr::map_chr(
.data$title,
~ sub("^\\S* ", "", .x) # Remove string up until first space
),
orcid = ifelse( # Move ORCID from path to separate column
!is.na(regexpr(orcid_regex, .data$path)),
regmatches(.data$path, regexpr(orcid_regex, .data$path)),
NA_character_
),
path = ifelse(
grepl(orcid_regex, .data$path),
NA_character_,
.data$path
),
role = .data$role
)

# Create creators list
creator_list <- purrr::transpose(creators)
creators <- contributors(x)

# Set creators
eml$dataset$creator <- create_eml_contributors(creator_list)
eml$dataset$creator <- create_eml_contributors(creators)

# Set contacts
contact_df <- dplyr::filter(creators, role == "contact")
contact_list <- purrr::transpose(contact_df)
if (length(contact_list) != 0) {
contacts <- create_eml_contributors(contact_list)
if (nrow(contact_df) != 0) {
contacts <- create_eml_contributors(contact_df)
} else {
contacts <- purrr::pluck(eml, "dataset", "creator", 1) # First creator
}
Expand Down
32 changes: 32 additions & 0 deletions man/contributors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/deployments.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/events.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/locations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/media.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/observations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/taxa.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading