usedf: Set or inspect the active data frame

View source: R/active.R

usedfR Documentation

Set or inspect the active data frame

Description

Makes a data frame the default data source for R4VN commands. When a bare object name is supplied, such as usedf(patient), the active data remains linked to that object. R4VN editing commands such as genvar(), replacevar(), labvar(), renvar(), dropvar(), keepvar(), and ordervar() then update the object itself as well as the active data.

Usage

usedf(data, clear = FALSE, quiet = FALSE)

Arguments

data

Optional data frame to make active.

clear

Logical; clear active data.

quiet

Logical; suppress status messages.

Details

If an expression rather than a bare object name is supplied, R4VN stores an unlinked working copy. Use newdata <- usedf() to retrieve that copy.

Value

The active data frame invisibly, or NULL after clearing.

Examples

patient <- data.frame(id = 1:3, age = c(20, 30, 40))
usedf(patient, quiet = TRUE)

# Editing active data also updates patient
genvar(age2 = age^2)
names(patient)

# Manual changes to patient are seen by later active-data commands
patient$age[1] <- 21
usedf(quiet = TRUE)$age

usedf(clear = TRUE, quiet = TRUE)

# Extended usage examples

patient <- data.frame(id = 1:4, age = c(20, 30, 40, 50))

# Set a visible object as active data. The object remains in Environment.
usedf(patient)
usedf()

# R4VN editing commands update both active data and patient.
genvar(age10 = age / 10, label = "Age in decades")
labvar(age, label = "Age in years")
names(patient)
if (interactive()) View(patient)

# Manual changes to patient are visible to later active-data commands.
patient$age[1] <- 21
sum1(age)

# An expression creates an unlinked working copy.
usedf(subset(patient, age >= 30))
genvar(age2 = age^2)
working_copy <- usedf()

# Return to the linked object or clear active data.
usedf(patient)
usedf(clear = TRUE)


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