Skip to content

Commit 7570d7d

Browse files
Merge pull request #40 from rOpenGov/24-FV22TOTA
Fix #24
2 parents c52b30f + f52d86f commit 7570d7d

13 files changed

+97
-49
lines changed

.lintr

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
linters: linters_with_defaults() # see vignette("lintr")
1+
linters: linters_with_defaults(
2+
cyclocomp_linter(complexity_limit = 17L))
23
encoding: "UTF-8"
34
exclusions: list(
45
"tests")

DESCRIPTION

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Suggests:
2525
testthat
2626
Encoding: UTF-8
2727
Roxygen: list(markdown = TRUE)
28+
Config/testthat/edition: 3
29+
Config/testthat/parallel: true
2830
RoxygenNote: 7.3.2
2931
X-schema.org-isPartOf: http://ropengov.org/
3032
X-schema.org-keywords: ropengov

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#' requested.
1414
#' @param format character value. "CSV" or "BULK". If you choose BULK then you
1515
#' need to select a value for each of the parameters.
16-
#' @param value_presentation For now, "value" or "default"
16+
#' @param value_presentation For now, "value" or "default". When a table with
17+
#' observations that have the same name, this is automatically changed to
18+
#' CodeAndValue.
1719
#' @export
1820
#' @family Data retrival functions
1921
#' @importFrom utils read.csv read.csv2
@@ -47,6 +49,11 @@ dst_get_data <- function(table,
4749
}
4850
}
4951

52+
# If meta_data is NULL then get it automatically
53+
if (is.null(meta_data)) {
54+
meta_data <- dst_meta(table, lang = lang)
55+
}
56+
5057
# Force the names to be uppercase to match requirements from API
5158
names(query) <- toupper(names(query))
5259
dst_names <- names(query)
@@ -60,6 +67,11 @@ dst_get_data <- function(table,
6067
format = format
6168
)
6269

70+
# If overlaps in values are detected use CodeAndValue as presentation
71+
if (dst_determine_overlaps(meta_data)) {
72+
value_presentation <- "CodeAndValue"
73+
}
74+
6375
query$valuePresentation <- value_presentation
6476
query$lang <- lang
6577

@@ -108,8 +120,14 @@ dst_get_data <- function(table,
108120
} else {
109121
stop("You haven't selected an appropiate language ('da' or 'en'")
110122
}
123+
111124
names(dst_data) <- c(dst_names, "value")
112125

126+
# Remove the code
127+
if (dst_determine_overlaps(meta_data)) {
128+
dst_data$TID <- sapply(stringr::str_split(dst_data$TID, "\\s+"), `[`, 2)
129+
}
130+
113131
# Parse the dates if param is TRUE
114132
if (parse_dst_tid) {
115133
dst_data$TID <- dst_date_parse(dst_date = dst_data$TID)

data-raw/dst_tables.R

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
tables <- dst_get_tables(lang = "en")
22

33
usethis::use_data(tables, overwrite = TRUE)
4-
5-
# tables_da <- dst_get_tables(lang = "da")
6-
#
7-
# usethis::use_data(tables_da, overwrite = TRUE)

man/dst_get_data.Rd

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-dst_date_parse.R

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,49 @@
1-
context("dst_date_parse")
2-
31
test_that("dst_date_parse gives the correct class.", {
4-
2+
53
exp_dates <- c("POSIXct", "POSIXt")
6-
4+
75
# Daily
86
expect_equal(class(dst_date_parse(dst_date = "2000M01D01")), exp_dates)
97
expect_equal(class(dst_date_parse(dst_date = c("2000M01D20", "2000M02D21", "2000M03D23", "2000M04D24"))), exp_dates)
10-
8+
119
# Monthly
1210
expect_equal(class(dst_date_parse(dst_date = "2000M01")), exp_dates)
1311
expect_equal(class(dst_date_parse(dst_date = c("2000M01", "2000M02", "2000M03", "2000M04", "2000M10", "2000M11"))), exp_dates)
14-
12+
1513
# Quarterly
1614
expect_equal(class(dst_date_parse(dst_date = "2000Q1")), exp_dates)
1715
expect_equal(class(dst_date_parse(dst_date = "2000Q2")), exp_dates)
1816
expect_equal(class(dst_date_parse(dst_date = "2000Q3")), exp_dates)
1917
expect_equal(class(dst_date_parse(dst_date = "2000Q4")), exp_dates)
2018
expect_equal(class(dst_date_parse(dst_date = c("2000Q1", "2000Q2", "2000Q3", "2000Q4"))), exp_dates)
21-
19+
2220
# Yearly
2321
expect_equal(class(dst_date_parse(dst_date = "2000")), exp_dates)
2422
expect_equal(class(dst_date_parse(dst_date = c("2000", "2000", "2000", "2000"))), exp_dates)
25-
23+
2624
expect_equal(class(dst_date_parse(dst_date = c("2015H1", "2015H2"))), exp_dates)
2725
expect_equal(as.character(dst_date_parse(dst_date = c("2015H1", "2015H2"))), c("2015-01-01", "2015-07-01"))
28-
26+
2927
})
3028

3129

3230
test_that("Test that dst_date_parse stops when the input is bad.", {
33-
31+
3432
# Daily
3533
expect_error(dst_date_parse(dst_date = "2000M01D35"))
3634
expect_error(dst_date_parse(dst_date = "2000M10D40"))
37-
35+
3836
# Monthly
3937
expect_error(dst_date_parse(dst_date = "20000M01"))
4038
expect_error(dst_date_parse(dst_date = "2000M101"))
4139
expect_error(dst_date_parse(dst_date = "2000M13"))
42-
40+
4341
# Quarterly
4442
expect_error(dst_date_parse(dst_date = "2000Q0"))
4543
expect_error(dst_date_parse(dst_date = "2000Q5"))
46-
44+
4745
# Yearly
4846
expect_error(dst_date_parse(dst_date = "20000"))
4947
expect_error(dst_date_parse(dst_date = "200"))
50-
48+
5149
})
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+
})

tests/testthat/test-dst_get_data.R

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
2-
context("dst_get_data")
3-
41
test_that("dst_get_data returns an error when the limit is reached.", {
5-
2+
63
expect_error(dst_get_data(table = "folk1",
7-
query = list(CIVILSTAND = "*",
8-
STATSB = "*",
4+
query = list(CIVILSTAND = "*",
5+
STATSB = "*",
96
HERKOMST = c("Personer med dansk oprindelse", "Efterkommere"),
107
TID = "*",
118
ALDER = "*"), lang = "da"))
12-
9+
1310
})
1411

1512

1613
test_that("dst_get_data is parsing the data correctly when 'en' and 'da' are selected as language. The API returns decimal numbers with both , and .", {
17-
14+
1815
expect_equal(class(dst_get_data("AUP01", ALDER = "*", TID = "*", lang = "da", format = "CSV")$value), "numeric")
19-
16+
2017
})
2118

2219
test_that("dst_get_data fails with the BULK format when not all parameters have values.", {
23-
20+
2421
expect_error(dst_get_data("AUP01", ALDER = "*", TID = "*", lang = "da", format = "BULK"))
25-
22+
2623
})
2724

2825

tests/testthat/test-dst_get_tables.R

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
2-
context("dst_get_tables")
3-
41
test_that("dst_get_tables return a data.frame",{
5-
2+
63
expect_equal(class(dst_get_tables(lang = "da")), "data.frame")
74
expect_equal(class(dst_get_tables(lang = "en")), "data.frame")
8-
5+
96
})
107

118
test_that("dst_get_tables failes with wrong language input",{
12-
9+
1310
expect_error(dst_get_tables(lang = "uk"))
1411
expect_error(dst_get_tables(lang = "no"))
1512
})

tests/testthat/test-dst_query_match.R

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
2-
3-
context("dst_query_match")
4-
51
test_that("The function fails when mandatory values are not supplied.",{
6-
2+
73
expect_error(dst_query_match(table = "NRHP", meta_data = NULL, lang = "en", query = list(TRANSAKT = "P.1 Output", Tid = "1993")))
8-
})
4+
})

tests/testthat/test-dst_value_limit.R

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
2-
3-
4-
context("dst_value_limit")
5-
61
test_that("dst_value_limit returns the correct value.", {
72

83
expect_equal(dst_value_limit(query = list(CIVILSTAND = "*",

tests/testthat/test-fv22tota.R

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
test_that("Tables with identifyers with the same name but different ids are parsed properly", {
2+
# This tests that the issue in https://github.com/rOpenGov/dkstat/issues/24
3+
# does not occur
4+
5+
table <- dst_get_data(
6+
table = "FV22TOTA",
7+
VALRES = "*",
8+
OMRÅDE = "*",
9+
Tid = "*",
10+
lang = "da",
11+
format = "BULK"
12+
)
13+
14+
expect_equal(nrow(unique(table)), nrow(table))
15+
expect_equal(nrow(table) - nrow(unique(table)), 0)
16+
17+
})

0 commit comments

Comments
 (0)