@@ -172,7 +172,7 @@ redcap_project_info_read <- function(
172
172
handle_httr = handle_httr
173
173
)
174
174
175
- col_types <- readr :: cols(
175
+ all_col_types <- readr :: cols(
176
176
project_id = readr :: col_integer(),
177
177
project_title = readr :: col_character(),
178
178
creation_time = readr :: col_datetime(format = " " ),
@@ -204,14 +204,40 @@ redcap_project_info_read <- function(
204
204
205
205
if (kernel $ success ) {
206
206
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
+ },
215
241
216
242
# Don't print the warning in the try block. Print it below,
217
243
# where it's under the control of the caller.
0 commit comments