diff --git a/R/redcap-metadata-read.R b/R/redcap-metadata-read.R index 9b424971..11e4f522 100644 --- a/R/redcap-metadata-read.R +++ b/R/redcap-metadata-read.R @@ -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( @@ -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 +} diff --git a/man/to_api_array.Rd b/man/to_api_array.Rd new file mode 100644 index 00000000..22abef13 --- /dev/null +++ b/man/to_api_array.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/redcap-metadata-read.R +\name{to_api_array} +\alias{to_api_array} +\title{Convert a vector to the array format expected by the REDCap API} +\usage{ +to_api_array(x, arr_name) +} +\arguments{ +\item{x}{A vector to convert to array format} + +\item{arr_name}{A string containing the name of the API request parameter for +the array} +} +\value{ +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. +} +\description{ +Utility function to convert a vector into the array format expected by the +REDCap API. +}