Skip to content

Commit ccd6d16

Browse files
Use CodeAndValue when overlaps are detected in any columns
Fixes #24
1 parent a3ceb7a commit ccd6d16

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

R/dst_determine_overlaps.R

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#' Helper function to determine wether or not to include the id in a variable
2+
#' option
3+
#'
4+
#' @param meta_data Meta data object for the table of inquiry
5+
#' @noRd
6+
dst_determine_overlaps <- function(meta_data) {
7+
# Get variable names
8+
var_names <- get_vars(meta_data)
9+
10+
# Get options for all variable names
11+
options <- get_var_options(meta_data, var_names)
12+
13+
# Index over all vars to determine if there is duplicates
14+
dup <- list()
15+
16+
for (i in seq_along(var_names)) {
17+
dup[i] <- length(
18+
options[[var_names[i]]]
19+
) == length(
20+
unique(options[[var_names[i]]])
21+
)
22+
}
23+
24+
# If any of the option/vars include duplicates, we should include the id
25+
return(any(unlist(dup)))
26+
}

R/dst_get_data.R

+15
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ dst_get_data <- function(table,
4747
}
4848
}
4949

50+
# If meta_data is NULL then get it automatically
51+
if (is.null(meta_data)) {
52+
meta_data <- dst_meta(table, lang = lang)
53+
}
54+
5055
# Force the names to be uppercase to match requirements from API
5156
names(query) <- toupper(names(query))
5257
dst_names <- names(query)
@@ -60,6 +65,11 @@ dst_get_data <- function(table,
6065
format = format
6166
)
6267

68+
# If overlaps in values are detected use CodeAndValue as presentation
69+
if (dst_determine_overlaps(meta_data)) {
70+
value_presentation <- "CodeAndValue"
71+
}
72+
6373
query$valuePresentation <- value_presentation
6474
query$lang <- lang
6575

@@ -107,6 +117,11 @@ dst_get_data <- function(table,
107117
}
108118
names(dst_data) <- c(dst_names, "value")
109119

120+
# Remove the code
121+
if (dst_determine_overlaps(meta_data)) {
122+
dst_data$TID <- sapply(stringr::str_split(dst_data$TID, "\\s+"), `[`, 2)
123+
}
124+
110125
# Parse the dates if param is TRUE
111126
if (parse_dst_tid) {
112127
dst_data$TID <- dst_date_parse(dst_date = dst_data$TID)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test_that("Overlap detection works", {
2+
expect_true(dst_determine_overlaps(dst_meta("FV22TOTA")))
3+
})

0 commit comments

Comments
 (0)