Skip to content

Commit 518925c

Browse files
committed
Allow specifying format for metadata downloads to allow for json download when csv fails
1 parent df6263f commit 518925c

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

R/redcap-metadata-read.R

+14-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#' project. Required.
99
#' @param token The user-specific string that serves as the password for a
1010
#' project. Required.
11+
#' @param format Format to download the metadata in. The default is "csv".
1112
#' @param forms An array, where each element corresponds to the REDCap form
1213
#' of the desired fields. Optional.
1314
#' @param forms_collapsed A single string, where the desired forms are
@@ -71,6 +72,7 @@
7172
redcap_metadata_read <- function(
7273
redcap_uri,
7374
token,
75+
format = "csv",
7476
forms = NULL,
7577
forms_collapsed = "",
7678
fields = NULL,
@@ -95,7 +97,7 @@ redcap_metadata_read <- function(
9597
post_body <- list(
9698
token = token,
9799
content = "metadata",
98-
format = "csv",
100+
format = format,
99101
forms = forms_collapsed,
100102
fields = fields_collapsed
101103
)
@@ -112,11 +114,17 @@ redcap_metadata_read <- function(
112114

113115
try(
114116
# Convert the raw text to a dataset.
115-
ds <-
116-
readr::read_csv(
117-
kernel$raw_text,
118-
col_types = col_types
119-
),
117+
if (format == "csv") {
118+
ds <-
119+
readr::read_csv(
120+
kernel$raw_text,
121+
col_types = col_types
122+
)
123+
} else if (format == "json") {
124+
json_df <- jsonlite::fromJSON(kernel$raw_text)
125+
json_tibble <- tibble::as_tibble(json_df)
126+
ds <- dplyr::mutate_all(json_tibble, ~ dplyr::na_if(.x, ""))
127+
},
120128
# Don't print the warning in the try block. Print it below,
121129
# where it's under the control of the caller.
122130
silent = TRUE

R/redcap-read.R

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#' project. Required.
2020
#' @param token The user-specific string that serves as the password for a
2121
#' project. Required.
22+
#' @param format Format to download the metadata in. The default is "csv".
2223
#' @param records An array, where each element corresponds to the ID of a
2324
#' desired record. Optional.
2425
#' @param records_collapsed A single string, where the desired ID values are
@@ -173,6 +174,7 @@ redcap_read <- function(
173174
continue_on_error = FALSE,
174175
redcap_uri,
175176
token,
177+
format = "csv",
176178
records = NULL, records_collapsed = "",
177179
fields = NULL, fields_collapsed = "",
178180
forms = NULL, forms_collapsed = "",
@@ -242,7 +244,8 @@ redcap_read <- function(
242244
redcap_uri = redcap_uri,
243245
token = token,
244246
verbose = verbose,
245-
config_options = config_options
247+
config_options = config_options,
248+
format = format
246249
)
247250

248251
# if (!metadata$success) {

0 commit comments

Comments
 (0)