Skip to content

Commit

Permalink
Merge pull request #445 from ezraporter/fix-metadata-forms-filter
Browse files Browse the repository at this point in the history
Fix metadata forms filter
  • Loading branch information
wibeasley authored Oct 22, 2022
2 parents a6fadbb + a84643b commit da7a486
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
37 changes: 34 additions & 3 deletions R/redcap-metadata-read.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,21 @@ redcap_metadata_read <- function(

token <- sanitize_token(token)
fields_collapsed <- collapse_vector(fields)
fields_array <- to_api_array(fields, "fields")
forms_collapsed <- collapse_vector(forms)
forms_array <- to_api_array(forms, "forms")
verbose <- verbose_prepare(verbose)

post_body <- list(
token = token,
content = "metadata",
format = "json",
forms = forms_collapsed,
fields = fields_collapsed
format = "json"
)

# append forms and fields arrays in format expected by REDCap API
# If either is NULL nothing will be appended
post_body <- c(post_body, fields_array, forms_array)

# This is the important call that communicates with the REDCap server.
kernel <-
kernel_api(
Expand Down Expand Up @@ -179,3 +183,30 @@ redcap_metadata_read <- function(
raw_text = kernel$raw_text
)
}

#' @title
#' Convert a vector to the array format expected by the REDCap API
#'
#' @description
#' Utility function to convert a vector into the array format expected by the
#' REDCap API.
#'
#' @param x A vector to convert to array format
#' @param arr_name A string containing the name of the API request parameter for
#' the array
#'
#' @return
#' If \code{x} is not \code{NULL} a list is returned with one element for
#' each element of x in the format:
#' \code{list(`arr_name[0]` = x[1], `arr_name[1]` = x[2], ...)}. If \code{x} is
#' \code{NULL} then \code{NULL} is returned.
to_api_array <- function(x, arr_name) {
if (is.null(x)) {
return(NULL)
}

res <- as.list(x)
names(res) <- paste0(arr_name, "[", seq_along(res) - 1, "]")

res
}
24 changes: 24 additions & 0 deletions man/to_api_array.Rd

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

0 comments on commit da7a486

Please sign in to comment.