label-expectations {testdat} | R Documentation |
Expectations: labels
Description
Test whether variables in a data frame are labelled in a given way.
Usage
expect_labels(
vars,
val_labels = NULL,
var_label = NULL,
flt = TRUE,
data = get_testdata()
)
Arguments
vars |
< |
val_labels |
What value label check should be performed? One of:
|
var_label |
What variable label check should be performed? One of:
|
flt |
< |
data |
A data frame to test. The global test data is used by default. |
Value
expect_*()
functions are mainly called for their side effects. The
expectation signals its result (e.g. "success", "failure"), which is logged
by the current test reporter. In a non-testing
context the expectation will raise an error with class
expectation_failure
if it fails.
See Also
Other data expectations:
conditional-expectations
,
datacomp-expectations
,
date-expectations
,
exclusivity-expectations
,
expect_depends()
,
generic-expectations
,
pattern-expectations
,
proportion-expectations
,
text-expectations
,
uniqueness-expectations
,
value-expectations
Examples
df <- data.frame(
x = labelled::labelled(c("M", "M", "F"), c(Male = "M", Female = "F"), "Sex"),
y = labelled::labelled(c("M", "M", "F"), c(Male = "M", Female = "F", Other = "X")),
z = c("M", "M", "F")
)
# Check for a value-label pairing
try(expect_labels(x, c(Male = "M"), data = df))
# Check that two variables have the same values
expect_labels(x, labelled::val_labels(df$y), data = df) # N.B. This passes!
# Check for the presence of a particular label
try(expect_labels(x, "Male", data = df))
expect_labels(x, var_label = "Sex", data = df)
# Check that a variable is labelled at all
try(expect_labels(z, val_labels = TRUE, data = df))
try(expect_labels(z, var_label = TRUE, data = df))
# Check that a variable isn't labelled
expect_labels(z, val_labels = FALSE, data = df)
expect_labels(z, var_label = FALSE, data = df)