Skip to content

Commit afc6a97

Browse files
authored
Merge pull request #466 from the-mad-statter/dev
build col spec matching project info response
2 parents 67dca5a + 80af328 commit afc6a97

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

R/redcap-metadata-coltypes.R

+10
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,16 @@ redcap_metadata_internal <- function(
301301
)
302302
}
303303

304+
if (is.na(d_proj$has_repeating_instruments_or_events[1]))
305+
stop(
306+
sprintf(
307+
paste(
308+
"The REDCap instance at %s failed to report if the",
309+
"current project uses repeatable instruments or events."
310+
),
311+
redcap_uri
312+
)
313+
)
304314
if (d_proj$has_repeating_instruments_or_events[1]) {
305315
d_again <-
306316
d_again %>%

R/redcap-project-info-read.R

+35-9
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ redcap_project_info_read <- function(
172172
handle_httr = handle_httr
173173
)
174174

175-
col_types <- readr::cols(
175+
all_col_types <- readr::cols(
176176
project_id = readr::col_integer(),
177177
project_title = readr::col_character(),
178178
creation_time = readr::col_datetime(format = ""),
@@ -204,14 +204,40 @@ redcap_project_info_read <- function(
204204

205205
if (kernel$success) {
206206
try(
207-
# Convert the raw text to a dataset.
208-
ds <-
209-
readr::read_csv(
210-
file = I(kernel$raw_text),
211-
locale = locale,
212-
col_types = col_types,
213-
show_col_types = FALSE
214-
),
207+
{
208+
# Read column names returned by the API.
209+
present_names <-
210+
names(
211+
readr::read_csv(
212+
file = I(kernel$raw_text),
213+
locale = locale,
214+
n_max = 0,
215+
show_col_types = FALSE
216+
)
217+
)
218+
219+
# Build a column specification that matches the API response.
220+
col_types <- readr::cols()
221+
for(present_name in present_names)
222+
col_types$cols <- c(col_types$cols, all_col_types$cols[present_name])
223+
224+
# Convert the raw text to a dataset.
225+
ds <-
226+
readr::read_csv(
227+
file = I(kernel$raw_text),
228+
locale = locale,
229+
col_types = col_types,
230+
show_col_types = FALSE
231+
)
232+
233+
# Add any missing columns as NA.
234+
absent_names <- setdiff(names(all_col_types$cols), names(col_types$cols))
235+
for(absent_name in absent_names) {
236+
ds[absent_name] <- NA
237+
attr(ds, "spec")$cols <-
238+
c(attr(ds, "spec")$cols, all_col_types$cols[absent_name])
239+
}
240+
},
215241

216242
# Don't print the warning in the try block. Print it below,
217243
# where it's under the control of the caller.

0 commit comments

Comments
 (0)