Skip to content

Commit

Permalink
pass R CMD CHECK
Browse files Browse the repository at this point in the history
  • Loading branch information
PietrH committed Nov 27, 2023
1 parent 6ffccb3 commit 998bbfe
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 12 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]",
Expand All @@ -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)
Expand All @@ -24,4 +24,5 @@ Imports:
magrittr,
openssl,
purrr,
rlang,
stringr
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export(get_access_token)
export(get_fields)
export(get_records)
importFrom(magrittr,"%>%")
importFrom(rlang,.data)
15 changes: 9 additions & 6 deletions R/get_records.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)))) %>%

Expand Down Expand Up @@ -90,16 +93,16 @@ 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
records_recoded <-
records_no_lists %>%
dplyr::mutate(
dplyr::across(
all_of(fields_to_recode),
dplyr::all_of(fields_to_recode),
.names = "{.col}",
~recode_by_field(
.x,
Expand Down
7 changes: 7 additions & 0 deletions R/iassetR-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' @keywords internal
"_PACKAGE"

## usethis namespace: start
#' @importFrom rlang .data
## usethis namespace: end
NULL
12 changes: 8 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()`
Expand All @@ -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(),
Expand All @@ -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)
})
Expand Down
20 changes: 20 additions & 0 deletions man/iassetR-package.Rd

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

0 comments on commit 998bbfe

Please sign in to comment.