Skip to content

Commit

Permalink
Use CodeAndValue when overlaps are detected in any columns
Browse files Browse the repository at this point in the history
Fixes #24
  • Loading branch information
aleksanderbl29 committed Nov 28, 2024
1 parent a3ceb7a commit ccd6d16
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
26 changes: 26 additions & 0 deletions R/dst_determine_overlaps.R
Original file line number Diff line number Diff line change
@@ -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)))
}
15 changes: 15 additions & 0 deletions R/dst_get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-dst_determine_overlaps.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test_that("Overlap detection works", {
expect_true(dst_determine_overlaps(dst_meta("FV22TOTA")))
})

0 comments on commit ccd6d16

Please sign in to comment.