tests/testthat/test-vars-public-examples.R

test_that("vars selectors resolve through the public tab API", {
  dat <- data.frame(
    id = 1:5,
    age = c(31, 42, 38, 50, 46),
    sex = factor(c("F", "M", "F", "M", "F")),
    kt1 = 1:5,
    kt2 = 6:10,
    kt_total = 11:15,
    score_kt = 16:20
  )

  # Exact unprefixed variables use the same public automatic typing rule:
  # numeric -> mean (SD), factor/character/logical -> categorical.
  expect_no_error(t_auto <- tab(dat, vars = vars(age, sex, kt1), show = FALSE))
  expect_no_error(t_all <- tab(dat, vars = vars(.), show = FALSE))
  expect_no_error(t_kt <- tab(dat, vars = vars(`kt*`), show = FALSE))
  expect_no_error(t_no_id <- tab(dat, vars = vars(., -id), show = FALSE))
  expect_no_error(t_typed <- tab(
    dat, vars = vars(`c.kt*`, q.kt_total), show = FALSE
  ))

  expect_s3_class(t_auto, "r4vn_tab")
  expect_s3_class(t_all, "r4vn_tab")
  expect_s3_class(t_kt, "r4vn_tab")
  expect_s3_class(t_no_id, "r4vn_tab")
  expect_s3_class(t_typed, "r4vn_tab")
})

Try the R4VN package in your browser

Any scripts or data that you put into this service are public.

R4VN documentation built on Sept. 30, 2026, 5:13 p.m.