tests/testthat/test-active-data.R

test_that("usedf links active data to a visible object", {
  patient <- data.frame(age = c(20, 30, 40), sex = c(1, 2, 1))
  usedf(patient, quiet = TRUE)
  genvar(age2 = age^2)
  expect_true("age2" %in% names(patient))
  expect_equal(patient$age2, patient$age^2)
  patient$age[1] <- 21
  expect_equal(usedf(quiet = TRUE)$age[1], 21)
  usedf(clear = TRUE, quiet = TRUE)
})

test_that("an expression creates an unlinked active copy", {
  patient <- data.frame(age = c(20, 30, 40))
  usedf(subset(patient, age >= 30), quiet = TRUE)
  genvar(age2 = age^2)
  expect_false("age2" %in% names(patient))
  expect_true("age2" %in% names(usedf(quiet = TRUE)))
  usedf(clear = TRUE, quiet = TRUE)
})

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.