diff --git a/R/dst_determine_overlaps.R b/R/dst_determine_overlaps.R new file mode 100644 index 0000000..b10525f --- /dev/null +++ b/R/dst_determine_overlaps.R @@ -0,0 +1,26 @@ +#' Helper function to determine wether or not to include the id in a variable +#' option +#' +#' @param meta_data Meta data object for the table of inquiry +#' @noRd +dst_determine_overlaps <- function(meta_data) { + # Get variable names + var_names <- get_vars(meta_data) + + # Get options for all variable names + options <- get_var_options(meta_data, var_names) + + # Index over all vars to determine if there is duplicates + dup <- list() + + for (i in seq_along(var_names)) { + dup[i] <- length( + options[[var_names[i]]] + ) == length( + unique(options[[var_names[i]]]) + ) + } + + # If any of the option/vars include duplicates, we should include the id + return(any(unlist(dup))) +} diff --git a/R/dst_get_data.R b/R/dst_get_data.R index 2689ac1..3efe25d 100644 --- a/R/dst_get_data.R +++ b/R/dst_get_data.R @@ -47,6 +47,11 @@ dst_get_data <- function(table, } } + # If meta_data is NULL then get it automatically + if (is.null(meta_data)) { + meta_data <- dst_meta(table, lang = lang) + } + # Force the names to be uppercase to match requirements from API names(query) <- toupper(names(query)) dst_names <- names(query) @@ -60,6 +65,11 @@ dst_get_data <- function(table, format = format ) + # If overlaps in values are detected use CodeAndValue as presentation + if (dst_determine_overlaps(meta_data)) { + value_presentation <- "CodeAndValue" + } + query$valuePresentation <- value_presentation query$lang <- lang @@ -107,6 +117,11 @@ dst_get_data <- function(table, } names(dst_data) <- c(dst_names, "value") + # Remove the code + if (dst_determine_overlaps(meta_data)) { + dst_data$TID <- sapply(stringr::str_split(dst_data$TID, "\\s+"), `[`, 2) + } + # Parse the dates if param is TRUE if (parse_dst_tid) { dst_data$TID <- dst_date_parse(dst_date = dst_data$TID) diff --git a/tests/testthat/test-dst_determine_overlaps.R b/tests/testthat/test-dst_determine_overlaps.R new file mode 100644 index 0000000..a68fbf7 --- /dev/null +++ b/tests/testthat/test-dst_determine_overlaps.R @@ -0,0 +1,3 @@ +test_that("Overlap detection works", { + expect_true(dst_determine_overlaps(dst_meta("FV22TOTA"))) +})