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

build col spec matching project info response #466

Merged
merged 1 commit into from
Feb 6, 2023
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
build col spec matching project info response
  • Loading branch information
the-mad-statter committed Feb 4, 2023
commit 80af328068a241b289fed000924d4edd7a38a996
10 changes: 10 additions & 0 deletions R/redcap-metadata-coltypes.R
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,16 @@ redcap_metadata_internal <- function(
)
}

if (is.na(d_proj$has_repeating_instruments_or_events[1]))
stop(
sprintf(
paste(
"The REDCap instance at %s failed to report if the",
"current project uses repeatable instruments or events."
),
redcap_uri
)
)
if (d_proj$has_repeating_instruments_or_events[1]) {
d_again <-
d_again %>%
Expand Down
44 changes: 35 additions & 9 deletions R/redcap-project-info-read.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ redcap_project_info_read <- function(
handle_httr = handle_httr
)

col_types <- readr::cols(
all_col_types <- readr::cols(
project_id = readr::col_integer(),
project_title = readr::col_character(),
creation_time = readr::col_datetime(format = ""),
Expand Down Expand Up @@ -204,14 +204,40 @@ redcap_project_info_read <- function(

if (kernel$success) {
try(
# Convert the raw text to a dataset.
ds <-
readr::read_csv(
file = I(kernel$raw_text),
locale = locale,
col_types = col_types,
show_col_types = FALSE
),
{
# Read column names returned by the API.
present_names <-
names(
readr::read_csv(
file = I(kernel$raw_text),
locale = locale,
n_max = 0,
show_col_types = FALSE
)
)

# Build a column specification that matches the API response.
col_types <- readr::cols()
for(present_name in present_names)
col_types$cols <- c(col_types$cols, all_col_types$cols[present_name])

# Convert the raw text to a dataset.
ds <-
readr::read_csv(
file = I(kernel$raw_text),
locale = locale,
col_types = col_types,
show_col_types = FALSE
)

# Add any missing columns as NA.
absent_names <- setdiff(names(all_col_types$cols), names(col_types$cols))
for(absent_name in absent_names) {
ds[absent_name] <- NA
attr(ds, "spec")$cols <-
c(attr(ds, "spec")$cols, all_col_types$cols[absent_name])
}
},

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