Skip to content

Commit

Permalink
Add verbose option to metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
EeethB committed Dec 5, 2023
1 parent e718be3 commit ab82633
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 9 deletions.
8 changes: 7 additions & 1 deletion R/label.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
xportr_label <- function(.df,
metadata = NULL,
domain = NULL,
verbose = getOption("xportr.label_verbose", "none"),
verbose = NULL,
metacore = deprecated()) {
if (!missing(metacore)) {
lifecycle::deprecate_warn(
Expand Down Expand Up @@ -101,6 +101,12 @@ xportr_label <- function(.df,
# Check any variables missed in metadata but present in input data ---
miss_vars <- setdiff(names(.df), metadata[[variable_name]])

# Verbose should use an explicit verbose option first, then the value set in
# metadata, and finally fall back to the option value
verbose <- verbose %||%
attr(.df, "_xportr.df_verbose_") %||%
getOption("xportr.label_verbose", "none")

label_log(miss_vars, verbose)

label <- metadata[[variable_label]]
Expand Down
8 changes: 7 additions & 1 deletion R/length.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
xportr_length <- function(.df,
metadata = NULL,
domain = NULL,
verbose = getOption("xportr.length_verbose", "none"),
verbose = NULL,
metacore = deprecated()) {
if (!missing(metacore)) {
lifecycle::deprecate_warn(
Expand Down Expand Up @@ -108,6 +108,12 @@ xportr_length <- function(.df,
# Check any variables missed in metadata but present in input data ---
miss_vars <- setdiff(names(.df), metadata[[variable_name]])

# Verbose should use an explicit verbose option first, then the value set in
# metadata, and finally fall back to the option value
verbose <- verbose %||%
attr(.df, "_xportr.df_verbose_") %||%
getOption("xportr.length_verbose", "none")

length_log(miss_vars, verbose)

length <- metadata[[variable_length]]
Expand Down
8 changes: 7 additions & 1 deletion R/order.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
xportr_order <- function(.df,
metadata = NULL,
domain = NULL,
verbose = getOption("xportr.order_verbose", "none"),
verbose = NULL,
metacore = deprecated()) {
if (!missing(metacore)) {
lifecycle::deprecate_warn(
Expand Down Expand Up @@ -122,6 +122,12 @@ xportr_order <- function(.df,
# Used in warning message for how many vars have been moved
reorder_vars <- names(df_re_ord)[names(df_re_ord) != names(.df)]

# Verbose should use an explicit verbose option first, then the value set in
# metadata, and finally fall back to the option value
verbose <- verbose %||%
attr(.df, "_xportr.df_verbose_") %||%
getOption("xportr.order_verbose", "none")

# Function is located in messages.R
var_ord_msg(reorder_vars, names(drop_vars), verbose)

Expand Down
6 changes: 5 additions & 1 deletion man/xportr_metadata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/xportr_type.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tests/testthat/test-length.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ test_that("xportr_length: Impute character lengths based on class", {
expect_message("Variable lengths missing from metadata") %>%
expect_message("lengths resolved") %>%
expect_attr_width(c(7, 199, 200, 200, 8))

adsl %>%
xportr_metadata(metadata, verbose = "none") %>%
xportr_length() %>%
expect_message("Variable lengths missing from metadata") %>%
expect_message("lengths resolved") %>%
expect_attr_width(c(7, 199, 200, 200, 8))
})

test_that("xportr_length: Throws message when variables not present in metadata", {
Expand All @@ -144,6 +151,12 @@ test_that("xportr_length: Throws message when variables not present in metadata"
expect_message("Variable lengths missing from metadata") %>%
expect_message("lengths resolved") %>%
expect_message(regexp = "Problem with `y`")

xportr_metadata(adsl, metadata, verbose = "message") %>%
xportr_length() %>%
expect_message("Variable lengths missing from metadata") %>%
expect_message("lengths resolved") %>%
expect_message(regexp = "Problem with `y`")
})

test_that("xportr_length: Metacore instance can be used", {
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-order.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ test_that("xportr_order: Variable ordering messaging is correct", {
expect_message("2 variables not in spec and moved to end") %>%
expect_message("Variable moved to end in `.df`: `a` and `z`") %>%
expect_message("All variables in dataset are ordered")

# Metadata versions
xportr_metadata(df, df_meta, verbose = "message") %>%
xportr_order() %>%
expect_message("All variables in specification file are in dataset") %>%
expect_condition("4 reordered in dataset") %>%
expect_message("Variable reordered in `.df`: `a`, `b`, `c`, and `d`")

xportr_metadata(df2, df_meta, verbose = "message") %>%
xportr_order() %>%
expect_message("2 variables not in spec and moved to end") %>%
expect_message("Variable moved to end in `.df`: `a` and `z`") %>%
expect_message("All variables in dataset are ordered")
})

test_that("xportr_order: Metadata order columns are coersed to numeric", {
Expand Down
20 changes: 16 additions & 4 deletions tests/testthat/test-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ test_that("xportr_type: Variable types are coerced as expected and can raise mes
(df3 <- suppressMessages(xportr_type(df, meta_example, verbose = "warn"))) %>%
expect_warning()

# Metadata version of the last statement
df %>%
xportr_metadata(meta_example, verbose = "warn") %>%
xportr_type() %>%
expect_warning()

expect_equal(purrr::map_chr(df3, class), c(
Subj = "numeric", Different = "character",
Val = "numeric", Param = "character"
Expand All @@ -77,6 +83,12 @@ test_that("xportr_type: Variable types are coerced as expected and can raise mes
expect_message("Variable type\\(s\\) in dataframe don't match metadata")
)

# Metadata version
df %>%
xportr_metadata(meta_example, verbose = "message") %>%
xportr_type() %>%
expect_message("Variable type\\(s\\) in dataframe don't match metadata")

expect_equal(purrr::map_chr(df4, class), c(
Subj = "numeric", Different = "character",
Val = "numeric", Param = "character"
Expand All @@ -100,12 +112,12 @@ test_that("xportr_metadata: Var types coerced as expected and raise messages", {
))

suppressMessages(
xportr_metadata(df, meta_example) %>% xportr_type(verbose = "stop")
xportr_metadata(df, meta_example, verbose = "stop") %>% xportr_type()
) %>%
expect_error()

suppressMessages(
df3 <- xportr_metadata(df, meta_example) %>% xportr_type(verbose = "warn")
df3 <- xportr_metadata(df, meta_example, verbose = "warn") %>% xportr_type()
) %>%
expect_warning()

Expand All @@ -116,8 +128,8 @@ test_that("xportr_metadata: Var types coerced as expected and raise messages", {

suppressMessages({
(
df4 <- xportr_metadata(df, meta_example) %>%
xportr_type(verbose = "message")
df4 <- xportr_metadata(df, meta_example, verbose = "message") %>%
xportr_type()
) %>%
expect_message("Variable type\\(s\\) in dataframe don't match metadata: `Subj` and `Val`")
})
Expand Down

0 comments on commit ab82633

Please sign in to comment.