Skip to content

Commit ba24410

Browse files
authored
Merge pull request #48 from steno-aarhus/feat/list-needed-vars
2 parents 0d68b77 + e761544 commit ba24410

8 files changed

+98
-17
lines changed

DESCRIPTION

+24-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,30 @@ Package: osdc
33
Title: Open Source Diabetes Classifier (OSCD) for Danish Registers
44
Version: 0.0.1.9000
55
Authors@R: c(
6-
# cre = maintainer, even though it translates to "creator"
7-
person(c("Luke", "William"), "Johnston", , "lwjohnst@gmail.com",
8-
comment = c(ORCID = "0000-0003-4169-2616"), role = c("aut", "cre")),
9-
person(c("Signe", "Kirk"), "Brødbæk", , "signekb@clin.au.dk", role = "aut"),
10-
person(c("Anders", "Aasted"), "Isaksen", , "andaas@rm.dk", role = "aut"),
11-
person("Steno Diabetes Center Aarhus", role = "cph"),
12-
person("Aarhus University", role = "cph")
6+
person(
7+
c("Luke", "William"), "Johnston",
8+
email = "lwjohnst@gmail.com",
9+
role = c("aut", "cre"),
10+
comment = c(ORCID = "0000-0003-4169-2616")
11+
),
12+
person(
13+
c("Signe", "Kirk"), "Brødbæk",
14+
email = "signekb@clin.au.dk",
15+
role = "aut"
16+
),
17+
person(
18+
c("Anders", "Aasted"), "Isaksen",
19+
email = "andaas@rm.dk",
20+
role = "aut"
21+
),
22+
person(
23+
"Steno Diabetes Center Aarhus",
24+
role = "cph"
25+
),
26+
person(
27+
"Aarhus University",
28+
role = "cph"
29+
)
1330
)
1431
Description: This classifier first identifies a population of individuals
1532
with any type of diabetes mellitus and then splits this population

R/get-variables.R

+23-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,31 @@
22
#' Get a list of the registers' abbreviations.
33
#'
44
#' @return A character string.
5-
#' @export
5+
#' @keywords internal
66
#'
77
#' @examples
88
#' get_register_abbrev()
99
get_register_abbrev <- function() {
10-
unique(required_variables$register_abbrev)
10+
unique(variable_description$register_abbrev)
11+
}
12+
13+
#' Get a list of required variables from a specific register.
14+
#'
15+
#' @param register The abbreviation of the register name. See list of
16+
#' abbreviations in [get_register_abbrev()].
17+
#'
18+
#' @return A character vector of variable names.
19+
#' @keywords internal
20+
#'
21+
#' @examples
22+
#' get_required_variables("bef")
23+
get_required_variables <- function(register) {
24+
if (!checkmate::test_scalar(register)) {
25+
cli::cli_abort("You are giving too many registers, please give only one.")
26+
}
27+
checkmate::assert_choice(register, get_register_abbrev())
28+
register <- rlang::arg_match(register, get_register_abbrev())
29+
variable_description |>
30+
dplyr::filter(.data$register_abbrev == register) |>
31+
dplyr::pull(.data$variable_name)
1132
}

R/sysdata.rda

-486 Bytes
Binary file not shown.

R/variable-description.R

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#' Variables from registers and their descriptions that are required for the
2+
#' osdc algorithm.
3+
#'
4+
#' @format ## `variable_description`
5+
#' A data frame with 39 rows and 6 columns:
6+
#' \describe{
7+
#' \item{register_name}{The official, full Danish name of the register.}
8+
#' \item{register_abbrev}{The official abbreviation for the register.}
9+
#' \item{variable_name}{The official name of the variable found in the register.}
10+
#' \item{years_covered}{The years when the variable is available from.}
11+
#' \item{danish_description}{The official description in Danish for the variable.}
12+
#' \item{english_description}{The translated description in English for the variable.}
13+
#' }
14+
#' @source Many of the details within the `variable_description` dataset come
15+
#' from the full official list of registers from Statistics Denmark (DST):
16+
#' <https://www.dst.dk/extranet/forskningvariabellister/Oversigt%20over%20registre.html>
17+
"variable_description"

R/verify-variables.R

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#' Verify that the dataset has the required variables for the algorithm.
22
#'
3+
#' Use this function inside another function within an `if` condition to provide an
4+
#' informative error message within the function used. This is done to make the
5+
#' error message more informative to the location that the error actually
6+
#' occurs, rather than within this function.
7+
#'
38
#' @param data The dataset to check.
4-
#' @param register The abbreviation of the register name. See list of
5-
#' abbreviations in [get_register_abbrev()].
9+
#' @inheritParams get_required_variables
610
#'
711
#' @return Either TRUE if the verification passes, or a character string if
812
#' there is an error.
@@ -16,10 +20,12 @@
1620
#' verify_required_variables(example_bef_data, "bef")
1721
verify_required_variables <- function(data, register) {
1822
checkmate::assert_choice(register, get_register_abbrev())
19-
expected_variables <- required_variables |>
20-
dplyr::filter(.data$register_abbrev == register) |>
21-
dplyr::pull(.data$variable_name)
23+
24+
# TODO: Consider using/looking into rlang::try_fetch() to provide contextual error messages.
25+
expected_variables <- get_required_variables(register)
26+
2227
actual_variables <- colnames(data)
28+
2329
checkmate::check_names(
2430
x = actual_variables,
2531
must.include = expected_variables

data-raw/variable-description.R

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
library(tidyverse)
44

5-
required_variables <- read_csv(here::here("data-raw/variable_description.csv")) |>
6-
select(register_abbrev = raw_register_filename, variable_name)
5+
variable_description <- here::here("data-raw/variable_description.csv") |>
6+
read_csv() |>
7+
select(
8+
register_name,
9+
register_abbrev = raw_register_filename,
10+
variable_name,
11+
years_covered,
12+
danish_description,
13+
english_description
14+
)
715

8-
usethis::use_data(required_variables, overwrite = TRUE, internal = TRUE)
16+
usethis::use_data(variable_description, overwrite = TRUE)

data/variable_description.rda

1.41 KB
Binary file not shown.

tests/testthat/test-get-variables.R

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
test_that("internal `get_` variable helper functions give correct output", {
2+
3+
# Should be character. Not sure if other tests are needed here.
4+
expect_type(get_register_abbrev(), "character")
5+
expect_type(get_required_variables("bef"), "character")
6+
7+
# Only able to use register ids that are real.
8+
expect_error(get_required_variables("fake"))
9+
10+
# Only allows a vector of one.
11+
expect_error(get_required_variables(c("bef", "atc")))
12+
})

0 commit comments

Comments
 (0)