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

Fix metadata forms filter #445

Merged
merged 2 commits into from
Oct 22, 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
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 @@ -177,3 +181,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.