Skip to main content

DARTER - Project 708421

Project-specific notes for project 708421

Notes for the DARTER project (708421) at Statistics Denmark (DST): initial setup with fastreg and the DARTER version of get_lpr_diagnoses() for LPR2 and LPR3.
Published

November 19, 2026

DARTER - Diabetes And inteRgenerational Transmission of hEalth determinants over the life couRse (project 708421).

This section is only for those working on the DARTER project. The content here builds on the general DST guide and adds the project-specific material.

Searchable variable and register overview for DARTER All variables and registers applied for in the project are collected in a searchable table: steno-aarhus.github.io/darter-project →

New to the project? Start with the general guide and return here: → Phase 1 - Plan your study → Phase 2 - R: the bare essentials → Phase 3 - Log in to DST

In this section

Page Contents
This page Setup and the DARTER version of get_lpr_diagnoses()
Register paths and datastores Confirmed paths and access methods for all registers on 708421
DARTER-specific pitfalls Quirks specific to this project

Initial setup for DARTER

Register data on DARTER is loaded with fastreg::read_register("name") - exactly as in the general guide. You just point fastreg at DARTER’s parquet folder once per script; see Loading templates.

Like every DST project, reinstall duckplyr at the start of each session: see Log in to DST.

Before running a script: verify that path_output at the top of each script points to your workspace folder (defined under Base paths in Register paths and datastores).

get_lpr_diagnoses() on DARTER

The general get_lpr_diagnoses() explains the pattern and how to call it. The DARTER version below differs in three ways: it uses DARTER’s renamed psychiatric keys (v_cpr, k_recnum, v_recnum), it reads the LPR_A tables by full path because read_register() does not resolve them on DARTER yet, and it uses the date cutover instead of the simple == "LPR3" filter. icd_codes is required: never fetch every diagnosis for the whole cohort.

See the full get_lpr_diagnoses() function
library(fastreg)
library(dplyr)

# icd_codes: 3-character ICD codes WITHOUT the D-prefix, e.g. c("F03", "G30"). REQUIRED (see warning).
# diagtypes: diagnosis TYPE - "A"=action, "B"=secondary, "G"=underlying/grundmorbus (LPR2 only). Default c("A", "B").
get_lpr_diagnoses <- function(pnr_vector, icd_codes, diagtypes = c("A", "B")) {
  #-------------------------------------------------------------
  # Open registers (LPR2 somatic + psychiatric + LPR3)
  #-------------------------------------------------------------
  lpr_adm   <- read_register("lpr_adm")   %>% rename_with(tolower)   # LPR2 somatic contacts
  lpr_diag  <- read_register("lpr_diag")  %>% rename_with(tolower)   # LPR2 somatic diagnoses
  psyk_adm  <- read_register("t_psyk_adm")  %>% rename_with(tolower) %>%
    rename(pnr = v_cpr, recnum = k_recnum)                            # LPR2 psychiatric contacts
  psyk_diag <- read_register("t_psyk_diag") %>% rename_with(tolower) %>%
    rename(recnum = v_recnum)                                          # LPR2 psychiatric diagnoses
  # LPR3: date cutover from DARTER pitfall 3, not the simple == "LPR3" filter, which
  # drops Jan-Mar 2019 here (lpr_adm stops 31 Dec 2018). read_register() does not
  # resolve lpr_a_kontakt/lpr_a_diagnose on DARTER yet, so use the full path.
  lpr_adm_last_date <- lpr_adm %>%
    summarise(max_date = max(d_inddto, na.rm = TRUE)) %>%
    collect() %>%
    pull(max_date)                                                    # where DARTER's LPR2 delivery stops
  lpr2_cutoff <- as.POSIXct(lpr_adm_last_date + 1, tz = "UTC")        # a datetime, made before the pipe (DST pitfall 4)
  lpr3_k    <- read_parquet_dataset("E:/workdata/708421/cleaned-data/parquet-registers/lpr_a_kontakt") %>%
    rename_with(tolower) %>%
    filter(lprindberetningssystem %in% c("LPR2", "LPR3")) %>%       # CRITICAL: drop LPR1/MiniPAS rows
    filter(!(lprindberetningssystem == "LPR2" &
             kont_starttidspunkt < !!lpr2_cutoff))                   # drop only LPR2 rows lpr_adm already has
  lpr3_d    <- read_parquet_dataset("E:/workdata/708421/cleaned-data/parquet-registers/lpr_a_diagnose") %>%
    rename_with(tolower) %>%
    filter(lprindberetningssystem %in% c("LPR2", "LPR3")) %>%
    semi_join(lpr3_k %>% select(dw_ek_kontakt), by = "dw_ek_kontakt") # keep diagnoses of the contacts kept above

  #-------------------------------------------------------------
  # LPR2 somatic
  #-------------------------------------------------------------
  lpr2_dx <- lpr_adm %>%
    semi_join(tibble(pnr = pnr_vector), by = "pnr", copy = TRUE) %>%   # only the cohort - the pattern is explained in Phase 9 (Hospital contacts)
    select(pnr, recnum, date_contact = d_inddto) %>%
    inner_join(
      lpr_diag %>% filter(c_diagtype %in% !!diagtypes) %>% select(recnum, c_diag, c_diagtype),
      by = "recnum"
    ) %>%
    filter(substr(c_diag, 2, 4) %in% !!icd_codes) %>%        # filter on codes BEFORE collect() - only relevant rows are pulled into R
    collect() %>%
    mutate(icd3 = substr(c_diag, 2, 4))                       # "DF03" -> "F03": strip the D-prefix

  #-------------------------------------------------------------
  # LPR2 psychiatric
  #-------------------------------------------------------------
  lpr2_psyk_dx <- psyk_adm %>%
    semi_join(tibble(pnr = pnr_vector), by = "pnr", copy = TRUE) %>%   # only the cohort - see Phase 9
    select(pnr, recnum, date_contact = d_inddto) %>%
    inner_join(
      psyk_diag %>% filter(c_diagtype %in% !!diagtypes) %>% select(recnum, c_diag, c_diagtype),
      by = "recnum"
    ) %>%
    filter(substr(c_diag, 2, 4) %in% !!icd_codes) %>%
    collect() %>%
    mutate(icd3 = substr(c_diag, 2, 4))

  #-------------------------------------------------------------
  # LPR3
  #-------------------------------------------------------------
  lpr3_dx <- lpr3_k %>%
    semi_join(tibble(pnr = pnr_vector), by = "pnr", copy = TRUE) %>%   # only the cohort - see Phase 9
    select(pnr, dw_ek_kontakt, date_contact = kont_starttidspunkt) %>%
    inner_join(
      lpr3_d %>%
        filter(diag_kode_type %in% !!diagtypes,               # NB: "G" (grundmorbus) exists only in LPR2, not LPR3
               is.na(senere_afkraeftet) | senere_afkraeftet != "Ja") %>%
        select(dw_ek_kontakt, c_diag = diag_kode, c_diagtype = diag_kode_type),
      by = "dw_ek_kontakt"
    ) %>%
    filter(substr(c_diag, 2, 4) %in% !!icd_codes) %>%
    collect() %>%
    mutate(date_contact = as.Date(date_contact),               # datetime → date
           icd3 = substr(c_diag, 2, 4))

  bind_rows(lpr2_dx, lpr2_psyk_dx, lpr3_dx)                   # return combined table
}

See also

Back to top