diff --git a/playgrounds/eav-playground-2.R b/playgrounds/eav-playground-2.R index 4d5ca84a..98764f82 100644 --- a/playgrounds/eav-playground-2.R +++ b/playgrounds/eav-playground-2.R @@ -12,7 +12,7 @@ requireNamespace("testit") redcap_uri <- "https://bbmc.ouhsc.edu/redcap/api/" token <- "9A81268476645C4E5F03428B8AC3AA7B" # PHI-free demo: simple static token <- "5007DC786DBE39CE77ED8DD0C68069A6" # PHI-free demo: Checkboxes 1 -token <- "CCB7E11837D41126D67C744F97389E04" # PHI-free demo: super-wide --3,000 columns +# token <- "CCB7E11837D41126D67C744F97389E04" # PHI-free demo: super-wide --3,000 columns # token <- "5C1526186C4D04AE0A0630743E69B53C" # PHI-free demo: super-wide #3--35,000 columns token <- "56F43A10D01D6578A46393394D76D88F" # PHI-free demo: Repeating Instruments --Sparse @@ -23,10 +23,6 @@ forms <- NULL records <- NULL #c("1") # ---- load-data --------------------------------------------------------------- -system.time( - ds_expected <- REDCapR::redcap_read_oneshot(redcap_uri, token, records=records)$data -) - system.time({ col_types <- REDCapR::redcap_metadata_coltypes(redcap_uri, token, print_col_types_to_console = FALSE) @@ -43,6 +39,10 @@ system.time({ ds_eav <- REDCapR:::redcap_read_eav_oneshot(redcap_uri, token, fields = desired_fields, records=records)$data }) +system.time( + ds_expected <- REDCapR::redcap_read_oneshot(redcap_uri, token, records=records, col_types=col_types)$data +) + testit::assert(ds_metadata$field_name == colnames(ds_expected)) testthat::expect_setequal( ds_metadata$field_name, colnames(ds_expected)) @@ -54,8 +54,10 @@ if (!"event_id" %in% colnames(ds_eav)) { .dummy_event <- FALSE } +.fields_plumbing <- c("record", "event_id", "redcap_repeat_instrument", "redcap_repeat_instance") .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) +.record_id_name <- ds_metadata$field_name[1] ds_eav_possible <- ds_eav %>% @@ -89,7 +91,7 @@ ds <- # 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), + id_cols = !!.fields_plumbing, names_from = .data$field_name, values_from = .data$value ) %>% @@ -100,9 +102,20 @@ if (.dummy_event) { ds$event_id <- NULL } +ds[[.record_id_name]] <- NULL ds <- ds %>% - readr::type_convert(col_types) + dplyr::rename( + !!.record_id_name := .data$record + ) %>% + readr::type_convert(col_types)# |> + # View() testit::assert(colnames(ds) == colnames(ds_expected)) testthat::expect_setequal(colnames(ds), colnames(ds_expected)) + +# ds_expected <- +# ds_expected %>% +# readr::type_convert(col_types=col_types) + +testthat::expect_equal(ds, ds_expected)