diff --git a/DESCRIPTION b/DESCRIPTION index 1684b0b..12fd260 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: iassetR -Title: What the Package Does (One Line, Title Case) +Title: Use the iAsset API in R. Version: 0.0.0.9000 Authors@R: c( person("Pieter", "Huybrechts", email = "pieter.huybrechts@inbo.be", @@ -9,7 +9,7 @@ Authors@R: c( role = c("aut"), comment = c(ORCID = "0000-0003-2015-5731")) ) -Description: What the package does (one paragraph). +Description: License: MIT + file LICENSE Encoding: UTF-8 Roxygen: list(markdown = TRUE) @@ -24,4 +24,5 @@ Imports: magrittr, openssl, purrr, + rlang, stringr diff --git a/NAMESPACE b/NAMESPACE index 2dc5821..cca531a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,3 +5,4 @@ export(get_access_token) export(get_fields) export(get_records) importFrom(magrittr,"%>%") +importFrom(rlang,.data) diff --git a/R/get_records.R b/R/get_records.R index a71482f..9150327 100644 --- a/R/get_records.R +++ b/R/get_records.R @@ -46,9 +46,12 @@ get_records <- function(inspection_name = "Vespa-Watch", purrr::map_dfr(~ purrr::discard(.x, function(x) all(x == ""))) %>% # drop value fields (paths to local images) dplyr::select(-dplyr::ends_with("_value")) %>% - # drop url suffix, now no longer neccesairy, breaks renaming later + # drop url suffix, now no longer necessary, breaks renaming later dplyr::rename_with(.fn = ~stringr::str_remove(.x, "_url"), - .cols = dplyr::ends_with("_url")) + .cols = dplyr::ends_with("_url")) %>% + # records are duplicated as multivalue fields get their own row, we can drop + # identical rows here + dplyr::distinct() # parse the API response so it's usable in analysis @@ -57,7 +60,7 @@ get_records <- function(inspection_name = "Vespa-Watch", records %>% dplyr::select(dplyr::where(is.list)) %>% # calculate the length of every element - dplyr::mutate(dplyr::across(everything(), + dplyr::mutate(dplyr::across(dplyr::everything(), .names = "{col}_len", .fns = ~purrr::map_int(.x,\(x) length(x)))) %>% @@ -90,8 +93,8 @@ get_records <- function(inspection_name = "Vespa-Watch", ## Select fields to be recoded based on their fieldtype fields_to_recode <- inspection_fields$fields %>% - dplyr::filter(fieldtype == "select" | fieldtype == "radio") %>% - dplyr::pull(id) %>% + dplyr::filter(.data$fieldtype == "select" | .data$fieldtype == "radio") %>% + dplyr::pull(.data$id) %>% unique() ## Recode values from id to the value returned in inspection_fields @@ -99,7 +102,7 @@ get_records <- function(inspection_name = "Vespa-Watch", records_no_lists %>% dplyr::mutate( dplyr::across( - all_of(fields_to_recode), + dplyr::all_of(fields_to_recode), .names = "{.col}", ~recode_by_field( .x, diff --git a/R/iassetR-package.R b/R/iassetR-package.R new file mode 100644 index 0000000..52c0c07 --- /dev/null +++ b/R/iassetR-package.R @@ -0,0 +1,7 @@ +#' @keywords internal +"_PACKAGE" + +## usethis namespace: start +#' @importFrom rlang .data +## usethis namespace: end +NULL diff --git a/R/utils.R b/R/utils.R index 7d5de4f..16b6f9e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -6,7 +6,9 @@ #' @param field_name Character. Name of field to be recoded, within across `cur_column()` #' @param inspection_fields named list, output of `get_fields()` #' -#' @return +#' @return function to be used by `dplyr::rename_with()` +#' +#' @noRd recode_by_field <- function(field, field_name = dplyr::cur_column(), inspection_fields = get_fields( @@ -15,7 +17,7 @@ recode_by_field <- function(field, )) { dplyr::recode(field, !!!dplyr::filter(inspection_fields$fields, - id == field_name)$options) + .data$id == field_name)$options) } #' Rename API columns with the label from `get_fields()` @@ -27,6 +29,8 @@ recode_by_field <- function(field, #' @param inspection_fields named list, output of `get_fields()` #' #' @return character vector of matching fieldlabel for id + suffix +#' +#' @noRd rename_by_id <- function(id, inspection_fields = get_fields( access_token = get_access_token(), @@ -38,8 +42,8 @@ rename_by_id <- function(id, suffix <- stringr::str_extract(id, "_[0-9]+") %>% stringr::str_replace_na(replacement = "") - dplyr::filter(inspection_fields$fields, id == pure_id) %>% - dplyr::pull(fieldlabel) %>% + dplyr::filter(inspection_fields$fields, .data$id == pure_id) %>% + dplyr::pull(.data$fieldlabel) %>% unique() %>% paste0(suffix) }) diff --git a/man/iassetR-package.Rd b/man/iassetR-package.Rd new file mode 100644 index 0000000..3de1e90 --- /dev/null +++ b/man/iassetR-package.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/iassetR-package.R +\docType{package} +\name{iassetR-package} +\alias{iassetR} +\alias{iassetR-package} +\title{iassetR: Use the iAsset API in R.} +\description{ + +} +\author{ +\strong{Maintainer}: Pieter Huybrechts \email{pieter.huybrechts@inbo.be} (\href{https://orcid.org/0000-0002-6658-6062}{ORCID}) + +Authors: +\itemize{ + \item Sander Devisscher \email{sander.devisscher@inbo.be} (\href{https://orcid.org/0000-0003-2015-5731}{ORCID}) +} + +} +\keyword{internal}