## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----load, message = FALSE----------------------------------------------------
library(data.table)
library(dtlog)

## ----vignette-option, include = FALSE-----------------------------------------
options(dtlog.log_from_packages = TRUE)

## ----data---------------------------------------------------------------------
set.seed(42)

visits <- data.table(
  id    = rep(1:20, each = 3),
  visit = rep(1:3, times = 20),
  date  = as.Date("2026-01-01") + sample(0:180, 60, replace = TRUE),
  sbp   = round(rnorm(60, mean = 132, sd = 16)),
  crp   = round(rexp(60, rate = 1 / 8), 1)
)
visits[sample(60, 6), sbp := NA_real_]

patients <- data.table(
  id  = 1:20,
  sex = sample(c("F", "M"), 20, replace = TRUE),
  age = sample(45:85, 20, replace = TRUE),
  arm = rep(c("control", "treatment"), each = 10)
)
patients <- patients[id != 7]   # one patient never made it into the registry

## ----io-----------------------------------------------------------------------
path <- tempfile(fileext = ".csv")
fwrite(patients, path)
patients <- fread(path)

## ----clean--------------------------------------------------------------------
visits <- na.omit(visits, cols = "sbp")

visits[, high := sbp >= 140]

visits[crp > 25, crp := NA_real_]

## ----rename-------------------------------------------------------------------
visits[, high := NULL]

setnames(visits, "sbp", "sbp_mmhg")

## ----join---------------------------------------------------------------------
merged <- merge(visits, patients, by = "id", all.x = TRUE)

## ----drop-unmatched-----------------------------------------------------------
merged <- merged[!is.na(arm)]

## ----join-i-------------------------------------------------------------------
setkey(patients, id)
joined <- visits[patients, on = "id"]

## ----reshape------------------------------------------------------------------
wide <- dcast(merged, id + arm ~ visit, value.var = "sbp_mmhg")

## ----summarise----------------------------------------------------------------
by_arm <- merged[, .(n        = .N,
                     patients = uniqueN(id),
                     mean_sbp = mean(sbp_mmhg),
                     mean_crp = mean(crp, na.rm = TRUE)),
                 by = arm]
by_arm

## ----i-vs-j-------------------------------------------------------------------
merged[age >= 65, .(mean_sbp = mean(sbp_mmhg)), by = arm]

## ----pipe, eval = getRversion() >= "4.3.0"------------------------------------
merged[arm == "treatment"] |>
  _[, .(mean_crp = mean(crp, na.rm = TRUE)), by = sex]

## ----transcript---------------------------------------------------------------
log_path <- tempfile(fileext = ".txt")
dt_log(log_path)

final <- merged[!is.na(crp)]
final[, crp_log := log(crp)]
summary_tbl <- final[, .(n = .N, mean_crp_log = mean(crp_log)), by = .(arm, sex)]

dt_log_end()

## ----transcript-show, comment = ""--------------------------------------------
cat(readLines(log_path), sep = "\n")

## ----transcript-file----------------------------------------------------------
dt_log_file()

## ----pause--------------------------------------------------------------------
dtlog_pause()
for (i in 1:3) merged[, tmp := i]
dtlog_resume()

merged[, tmp := NULL]

## ----compact------------------------------------------------------------------
options(dtlog.detail = "compact")
merged[, crp_high := crp > 10]
options(dtlog.detail = "full")

## ----display------------------------------------------------------------------
options(dtlog.display = list(function(x) cat("LOG |", x, "\n")))
elderly <- merged[age >= 70]

options(dtlog.display = NULL)  # back to message()

## ----summary------------------------------------------------------------------
dtlog_summary(merged)[1:2, .(id, arm)]

## ----cleanup, include = FALSE-------------------------------------------------
unlink(c(path, log_path))

