Skip to content

Commit

Permalink
crossing plumbing variables
Browse files Browse the repository at this point in the history
ref #420
  • Loading branch information
wibeasley committed Oct 2, 2022
1 parent 0075c26 commit af34dc3
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions playgrounds/eav-playground-2.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ token <- "56F43A10D01D6578A46393394D76D88F" # PHI-free demo: Repeating Instrume
# forms <- "health"
fields <- NULL
forms <- NULL
records <- NULL #c("1")

# ---- load-data ---------------------------------------------------------------
system.time(
ds_expected <- REDCapR::redcap_read_oneshot(redcap_uri, token)$data
ds_expected <- REDCapR::redcap_read_oneshot(redcap_uri, token, records=records)$data
)

system.time({
Expand All @@ -39,7 +40,7 @@ system.time({

desired_fields <- ds_metadata$field_name

ds_eav <- REDCapR:::redcap_read_eav_oneshot(redcap_uri, token, fields = desired_fields)$data
ds_eav <- REDCapR:::redcap_read_eav_oneshot(redcap_uri, token, fields = desired_fields, records=records)$data
})

testit::assert(ds_metadata$field_name == colnames(ds_expected))
Expand All @@ -48,13 +49,19 @@ testthat::expect_setequal( ds_metadata$field_name, colnames(ds_expected))
# ---- tweak-data --------------------------------------------------------------
if (!"event_id" %in% colnames(ds_eav)) {
ds_eav$event_id <- "dummy_1"
.dummy_event <- TRUE
} else {
.dummy_event <- FALSE
}

.fields_to_cross <- setdiff(ds_metadata$field_name, c("redcap_repeat_instrument", "redcap_repeat_instance"))
.fields_to_return <- c("record", "event_id", ds_metadata$field_name)

ds_eav_possible <-
ds_eav %>%
tidyr::expand(
tidyr::nesting(record, event_id),
tidyr::crossing(field_name = ds_metadata$field_name)
tidyr::crossing(field_name = .fields_to_cross)
)

ds_eav_2 <-
Expand All @@ -71,23 +78,31 @@ ds_eav_2 <-
) %>%
dplyr::right_join(ds_eav_possible, by = c("record", "event_id", "field_name"))

# . <- NULL # For the sake of avoiding an R CMD check note.
ds <-
ds_eav_2 %>%
dplyr::select(-.data$field_type, -.data$field_name_base) %>%
# dplyr::slice(1:46) %>%
# dplyr::group_by(record, redcap_repeat_instrument, redcap_repeat_instance, event_id, field_name) %>%
# dplyr::summarise(n = dplyr::n(), .groups = "drop") %>%
# dplyr::filter(n > 1L) %>%
# View()
# dplyr::select(-.data$redcap_repeat_instance) %>% # TODO: need a good fix for repeats
# tidyr::drop_na(event_id) %>% # TODO: need a good fix for repeats
tidyr::pivot_wider( # Everything else is considered an ID column
id_cols= c(.data$record, .data$event_id, .data$redcap_repeat_instrument, .data$redcap_repeat_instance),
names_from = .data$field_name,
values_from = .data$value
) %>%
dplyr::select(!!ds_metadata$field_name) %>%
readr::type_convert(col_types)
dplyr::select(!!.fields_to_return)
# dplyr::select(.data = ., !!intersect(variables_to_keep, colnames(.)))

if (.dummy_event) {
ds$event_id <- NULL
}

ds <-
ds %>%
readr::type_convert(col_types)

testit::assert(colnames(ds) == colnames(ds_expected))
testthat::expect_setequal(colnames(ds), colnames(ds_expected))

0 comments on commit af34dc3

Please sign in to comment.