diff --git a/NEWS.md b/NEWS.md index 0adb7867..3a66b267 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,6 +9,10 @@ ## Deprecation and Breaking Changes +* The `domain` argument for xportr functions will no longer be dynamically +determined by the name of the data frame passed as the .df argument. This was +done to make the use of xportr functions more explicit. (#182) + * The `label` argument from the `xportr_write()` function is deprecated in favor of the `metadata` argument. (#179) * The `metacore` argument, which was renamed to `metadata` in the following six xportr functions: (`xportr_df_label()`, `xportr_format()`, `xportr_label()`, `xportr_length()`, `xportr_order()`, and `xportr_type()`) in version `0.3.0` with a soft deprecation warning, has now been hard deprecated. Please update your code to use the new `metadata` argument in place of `metacore`. diff --git a/R/df_label.R b/R/df_label.R index 91ba0c36..b14d5ef4 100644 --- a/R/df_label.R +++ b/R/df_label.R @@ -38,7 +38,7 @@ #' label = c("Subject-Level Analysis", "Adverse Events Analysis") #' ) #' -#' adsl <- xportr_df_label(adsl, metadata) +#' adsl <- xportr_df_label(adsl, metadata, domain = "adsl") xportr_df_label <- function(.df, metadata = NULL, domain = NULL, @@ -53,10 +53,9 @@ xportr_df_label <- function(.df, domain_name <- getOption("xportr.df_domain_name") label_name <- getOption("xportr.df_label") - ## Common section to detect domain from argument or pipes + ## Common section to detect domain from argument or attribute - df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL) - domain <- get_domain(.df, df_arg, domain) + domain <- get_domain(.df, domain) if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain ## End of common section diff --git a/R/format.R b/R/format.R index 1b1a627d..4e6b908d 100644 --- a/R/format.R +++ b/R/format.R @@ -40,7 +40,7 @@ #' format = c(NA, "DATE9.") #' ) #' -#' adsl <- xportr_format(adsl, metadata) +#' adsl <- xportr_format(adsl, metadata, domain = "adsl") xportr_format <- function(.df, metadata = NULL, domain = NULL, @@ -56,10 +56,9 @@ xportr_format <- function(.df, format_name <- getOption("xportr.format_name") variable_name <- getOption("xportr.variable_name") - ## Common section to detect domain from argument or pipes + ## Common section to detect domain from argument or attribute - df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL) - domain <- get_domain(.df, df_arg, domain) + domain <- get_domain(.df, domain) if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain ## End of common section @@ -72,7 +71,7 @@ xportr_format <- function(.df, metadata <- metadata$var_spec } - if (domain_name %in% names(metadata)) { + if (domain_name %in% names(metadata) && !is.null(domain)) { metadata <- metadata %>% dplyr::filter(!!sym(domain_name) == domain & !is.na(!!sym(format_name))) } else { diff --git a/R/label.R b/R/label.R index 03a09348..6724e53d 100644 --- a/R/label.R +++ b/R/label.R @@ -55,7 +55,7 @@ #' label = c("Unique Subject Identifier", "Study Site Identifier", "Age", "Sex") #' ) #' -#' adsl <- xportr_label(adsl, metadata) +#' adsl <- xportr_label(adsl, metadata, domain = "adsl") xportr_label <- function(.df, metadata = NULL, domain = NULL, @@ -72,10 +72,9 @@ xportr_label <- function(.df, variable_name <- getOption("xportr.variable_name") variable_label <- getOption("xportr.label") - ## Common section to detect domain from argument or pipes + ## Common section to detect domain from argument or attribute - df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL) - domain <- get_domain(.df, df_arg, domain) + domain <- get_domain(.df, domain) if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain ## End of common section @@ -88,7 +87,7 @@ xportr_label <- function(.df, metadata <- metadata$var_spec } - if (domain_name %in% names(metadata)) { + if (domain_name %in% names(metadata) && !is.null(domain)) { metadata <- metadata %>% dplyr::filter(!!sym(domain_name) == domain) } else { diff --git a/R/length.R b/R/length.R index 1cd10980..21ea95d4 100644 --- a/R/length.R +++ b/R/length.R @@ -9,8 +9,8 @@ #' @param metadata A data frame containing variable level metadata. See #' 'Metadata' section for details. #' @param domain Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -#' the metadata object. If none is passed, then name of the dataset passed as -#' .df will be used. +#' the metadata object. If none is passed, then [xportr_metadata()] must be +#' called before hand to set the domain as an attribute of `.df`. #' @param verbose The action this function takes when an action is taken on the #' dataset or function validation finds an issue. See 'Messaging' section for #' details. Options are 'stop', 'warn', 'message', and 'none' @@ -62,7 +62,7 @@ #' length = c(10, 8) #' ) #' -#' adsl <- xportr_length(adsl, metadata) +#' adsl <- xportr_length(adsl, metadata, domain = "adsl") xportr_length <- function(.df, metadata = NULL, domain = NULL, @@ -79,10 +79,9 @@ xportr_length <- function(.df, variable_length <- getOption("xportr.length") variable_name <- getOption("xportr.variable_name") - ## Common section to detect domain from argument or pipes + ## Common section to detect domain from argument or attribute - df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL) - domain <- get_domain(.df, df_arg, domain) + domain <- get_domain(.df, domain) if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain ## End of common section @@ -95,7 +94,7 @@ xportr_length <- function(.df, metadata <- metadata$var_spec } - if (domain_name %in% names(metadata)) { + if (domain_name %in% names(metadata) && !is.null(domain)) { metadata <- metadata %>% filter(!!sym(domain_name) == domain) } else { diff --git a/R/metadata.R b/R/metadata.R index 1fdabc28..df206ef2 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -1,15 +1,18 @@ #' Set variable specifications and domain #' -#' Sets metadata for a dataset in a way that can be accessed by other xportr -#' functions. If used at the start of an xportr pipeline, it removes the need to -#' set metadata and domain at each step individually. For details on the format -#' of the metadata, see the 'Metadata' section for each function in question. +#' Sets metadata and/or domain for a dataset in a way that can be accessed by +#' other xportr functions. If used at the start of an xportr pipeline, it +#' removes the need to set metadata and domain at each step individually. For +#' details on the format of the metadata, see the 'Metadata' section for each +#' function in question. #' #' @inheritParams xportr_length #' #' @return `.df` dataset with metadata and domain attributes set #' @export #' +#' @rdname metadata +#' #' @examples #' #' metadata <- data.frame( @@ -37,11 +40,13 @@ #' xportr_type() %>% #' xportr_order() #' } -xportr_metadata <- function(.df, metadata, domain = NULL) { - ## Common section to detect domain from argument or pipes +xportr_metadata <- function(.df, metadata = NULL, domain = NULL) { + if (is.null(metadata) && is.null(domain)) { + stop("Must provide either metadata or domain argument") + } + ## Common section to detect domain from argument or attribute - df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL) - domain <- get_domain(.df, df_arg, domain) + domain <- get_domain(.df, domain) if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain ## End of common section diff --git a/R/order.R b/R/order.R index 0c8d322f..60bccbc8 100644 --- a/R/order.R +++ b/R/order.R @@ -58,7 +58,7 @@ #' order = 1:4 #' ) #' -#' adsl <- xportr_order(adsl, metadata) +#' adsl <- xportr_order(adsl, metadata, domain = "adsl") xportr_order <- function(.df, metadata = NULL, domain = NULL, @@ -75,10 +75,9 @@ xportr_order <- function(.df, order_name <- getOption("xportr.order_name") variable_name <- getOption("xportr.variable_name") - ## Common section to detect domain from argument or pipes + ## Common section to detect domain from argument or attribute - df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL) - domain <- get_domain(.df, df_arg, domain) + domain <- get_domain(.df, domain) if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain ## End of common section @@ -91,7 +90,7 @@ xportr_order <- function(.df, metadata <- metadata$ds_vars } - if (domain_name %in% names(metadata)) { + if (domain_name %in% names(metadata) && !is.null(domain)) { metadata <- metadata %>% dplyr::filter(!!sym(domain_name) == domain & !is.na(!!sym(order_name))) } else { diff --git a/R/support-test.R b/R/support-test.R index e12a6650..d223a6d6 100644 --- a/R/support-test.R +++ b/R/support-test.R @@ -180,6 +180,7 @@ multiple_vars_in_spec_helper2 <- function(FUN) { local_cli_theme() adsl %>% + xportr_metadata(domain = "adsl") %>% FUN(metadata) %>% testthat::expect_no_message(message = "There are multiple specs for the same variable name") } diff --git a/R/type.R b/R/type.R index 86d6bd15..8f45e326 100644 --- a/R/type.R +++ b/R/type.R @@ -96,10 +96,9 @@ xportr_type <- function(.df, numericTypes <- c(getOption("xportr.numeric_types"), "_numeric") format_name <- getOption("xportr.format_name") - ## Common section to detect domain from argument or pipes + ## Common section to detect domain from argument or attribute - df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL) - domain <- get_domain(.df, df_arg, domain) + domain <- get_domain(.df, domain) if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain ## End of common section @@ -113,7 +112,7 @@ xportr_type <- function(.df, metadata <- metadata$var_spec } - if (domain_name %in% names(metadata)) { + if (domain_name %in% names(metadata) && !is.null(domain)) { metadata <- metadata %>% filter(!!sym(domain_name) == domain) } diff --git a/R/utils-xportr.R b/R/utils-xportr.R index feb31195..38a29dd3 100644 --- a/R/utils-xportr.R +++ b/R/utils-xportr.R @@ -317,21 +317,18 @@ xpt_validate <- function(data) { return(err_cnd) } -#' Get the domain from argument or from magrittr's pipe (`%>%`) +#' Get the domain from argument or from the existing domain attr #' #' @return A string representing the domain #' @noRd -get_domain <- function(.df, df_arg, domain) { +get_domain <- function(.df, domain) { if (!is.null(domain) && !is.character(domain)) { abort(c("`domain` must be a vector with type .", x = glue("Instead, it has type <{typeof(domain)}>.") )) } - if (identical(df_arg, ".")) { - df_arg <- get_pipe_call() - } - result <- domain %||% attr(.df, "_xportr.df_arg_") %||% df_arg + result <- domain %||% attr(.df, "_xportr.df_arg_") result } diff --git a/R/write.R b/R/write.R index 0dd13541..c9005471 100644 --- a/R/write.R +++ b/R/write.R @@ -37,6 +37,7 @@ #' var_spec <- data.frame(dataset = "adsl", label = "Subject-Level Analysis Dataset") #' xportr_write(adsl, #' path = paste0(tempdir(), "/adsl.xpt"), +#' domain = "adsl", #' metadata = var_spec, #' strict_checks = FALSE #' ) @@ -51,10 +52,9 @@ xportr_write <- function(.df, name <- tools::file_path_sans_ext(basename(path)) - ## Common section to detect domain from argument or pipes + ## Common section to detect domain from argument or attribute - df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL) - domain <- get_domain(.df, df_arg, domain) + domain <- get_domain(.df, domain) if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain ## End of common section diff --git a/README.Rmd b/README.Rmd index 89d6e94b..2f422c1c 100644 --- a/README.Rmd +++ b/README.Rmd @@ -131,11 +131,12 @@ Each `xportr_` function has been written in a way to take in a part of the speci ```{r, warning = FALSE, message=FALSE, eval=TRUE} adsl %>% - xportr_type(var_spec, "ADSL", verbose = "warn") %>% - xportr_length(var_spec, "ADSL", verbose = "warn") %>% - xportr_label(var_spec, "ADSL", verbose = "warn") %>% - xportr_order(var_spec, "ADSL", verbose = "warn") %>% - xportr_format(var_spec, "ADSL") %>% + xportr_metadata(var_spec, "ADSL") %>% + xportr_type(verbose = "warn") %>% + xportr_length(verbose = "warn") %>% + xportr_label(verbose = "warn") %>% + xportr_order(verbose = "warn") %>% + xportr_format() %>% xportr_df_label(dataset_spec, "ADSL") %>% xportr_write("adsl.xpt") ``` diff --git a/README.md b/README.md index 147964b2..bebb06c8 100644 --- a/README.md +++ b/README.md @@ -138,11 +138,12 @@ We have suppressed the warning for the sake of brevity. ``` r adsl %>% - xportr_type(var_spec, "ADSL", verbose = "warn") %>% - xportr_length(var_spec, "ADSL", verbose = "warn") %>% - xportr_label(var_spec, "ADSL", verbose = "warn") %>% - xportr_order(var_spec, "ADSL", verbose = "warn") %>% - xportr_format(var_spec, "ADSL") %>% + xportr_metadata(var_spec, "ADSL") %>% + xportr_type(verbose = "warn") %>% + xportr_length(verbose = "warn") %>% + xportr_label(verbose = "warn") %>% + xportr_order(verbose = "warn") %>% + xportr_format() %>% xportr_df_label(dataset_spec, "ADSL") %>% xportr_write("adsl.xpt") ``` diff --git a/man/xportr_metadata.Rd b/man/metadata.Rd similarity index 66% rename from man/xportr_metadata.Rd rename to man/metadata.Rd index 592c6f45..658fe0a4 100644 --- a/man/xportr_metadata.Rd +++ b/man/metadata.Rd @@ -4,7 +4,7 @@ \alias{xportr_metadata} \title{Set variable specifications and domain} \usage{ -xportr_metadata(.df, metadata, domain = NULL) +xportr_metadata(.df, metadata = NULL, domain = NULL) } \arguments{ \item{.df}{A data frame of CDISC standard.} @@ -13,17 +13,18 @@ xportr_metadata(.df, metadata, domain = NULL) 'Metadata' section for details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then name of the dataset passed as -.df will be used.} +the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be +called before hand to set the domain as an attribute of \code{.df}.} } \value{ \code{.df} dataset with metadata and domain attributes set } \description{ -Sets metadata for a dataset in a way that can be accessed by other xportr -functions. If used at the start of an xportr pipeline, it removes the need to -set metadata and domain at each step individually. For details on the format -of the metadata, see the 'Metadata' section for each function in question. +Sets metadata and/or domain for a dataset in a way that can be accessed by +other xportr functions. If used at the start of an xportr pipeline, it +removes the need to set metadata and domain at each step individually. For +details on the format of the metadata, see the 'Metadata' section for each +function in question. } \examples{ diff --git a/man/xportr_df_label.Rd b/man/xportr_df_label.Rd index e5adca40..363c59c4 100644 --- a/man/xportr_df_label.Rd +++ b/man/xportr_df_label.Rd @@ -13,8 +13,8 @@ xportr_df_label(.df, metadata = NULL, domain = NULL, metacore = deprecated()) details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then name of the dataset passed as -.df will be used.} +the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be +called before hand to set the domain as an attribute of \code{.df}.} \item{metacore}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Previously used to pass metadata now renamed with \code{metadata}} @@ -55,5 +55,5 @@ metadata <- data.frame( label = c("Subject-Level Analysis", "Adverse Events Analysis") ) -adsl <- xportr_df_label(adsl, metadata) +adsl <- xportr_df_label(adsl, metadata, domain = "adsl") } diff --git a/man/xportr_format.Rd b/man/xportr_format.Rd index a4f06222..059fe168 100644 --- a/man/xportr_format.Rd +++ b/man/xportr_format.Rd @@ -13,8 +13,8 @@ xportr_format(.df, metadata = NULL, domain = NULL, metacore = deprecated()) 'Metadata' section for details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then name of the dataset passed as -.df will be used.} +the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be +called before hand to set the domain as an attribute of \code{.df}.} \item{metacore}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Previously used to pass metadata now renamed with \code{metadata}} @@ -58,5 +58,5 @@ metadata <- data.frame( format = c(NA, "DATE9.") ) -adsl <- xportr_format(adsl, metadata) +adsl <- xportr_format(adsl, metadata, domain = "adsl") } diff --git a/man/xportr_label.Rd b/man/xportr_label.Rd index a74137ed..6af7ad9a 100644 --- a/man/xportr_label.Rd +++ b/man/xportr_label.Rd @@ -19,8 +19,8 @@ xportr_label( 'Metadata' section for details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then name of the dataset passed as -.df will be used.} +the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be +called before hand to set the domain as an attribute of \code{.df}.} \item{verbose}{The action this function takes when an action is taken on the dataset or function validation finds an issue. See 'Messaging' section for @@ -84,5 +84,5 @@ metadata <- data.frame( label = c("Unique Subject Identifier", "Study Site Identifier", "Age", "Sex") ) -adsl <- xportr_label(adsl, metadata) +adsl <- xportr_label(adsl, metadata, domain = "adsl") } diff --git a/man/xportr_length.Rd b/man/xportr_length.Rd index 89fb5703..b7f3e818 100644 --- a/man/xportr_length.Rd +++ b/man/xportr_length.Rd @@ -19,8 +19,8 @@ xportr_length( 'Metadata' section for details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then name of the dataset passed as -.df will be used.} +the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be +called before hand to set the domain as an attribute of \code{.df}.} \item{verbose}{The action this function takes when an action is taken on the dataset or function validation finds an issue. See 'Messaging' section for @@ -81,5 +81,5 @@ metadata <- data.frame( length = c(10, 8) ) -adsl <- xportr_length(adsl, metadata) +adsl <- xportr_length(adsl, metadata, domain = "adsl") } diff --git a/man/xportr_order.Rd b/man/xportr_order.Rd index e8ea269c..de8ec9cd 100644 --- a/man/xportr_order.Rd +++ b/man/xportr_order.Rd @@ -19,8 +19,8 @@ xportr_order( 'Metadata' section for details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then name of the dataset passed as -.df will be used.} +the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be +called before hand to set the domain as an attribute of \code{.df}.} \item{verbose}{The action this function takes when an action is taken on the dataset or function validation finds an issue. See 'Messaging' section for @@ -88,5 +88,5 @@ metadata <- data.frame( order = 1:4 ) -adsl <- xportr_order(adsl, metadata) +adsl <- xportr_order(adsl, metadata, domain = "adsl") } diff --git a/man/xportr_type.Rd b/man/xportr_type.Rd index abfa41d8..440cf535 100644 --- a/man/xportr_type.Rd +++ b/man/xportr_type.Rd @@ -19,8 +19,8 @@ xportr_type( 'Metadata' section for details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then name of the dataset passed as -.df will be used.} +the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be +called before hand to set the domain as an attribute of \code{.df}.} \item{verbose}{The action this function takes when an action is taken on the dataset or function validation finds an issue. See 'Messaging' section for diff --git a/man/xportr_write.Rd b/man/xportr_write.Rd index b59e61bd..31c91c1e 100644 --- a/man/xportr_write.Rd +++ b/man/xportr_write.Rd @@ -23,8 +23,8 @@ used as \code{xpt} name.} 'Metadata' section for details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then name of the dataset passed as -.df will be used.} +the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be +called before hand to set the domain as an attribute of \code{.df}.} \item{strict_checks}{If TRUE, xpt validation will report errors and not write out the dataset. If FALSE, xpt validation will report warnings and continue @@ -60,6 +60,7 @@ adsl <- data.frame( var_spec <- data.frame(dataset = "adsl", label = "Subject-Level Analysis Dataset") xportr_write(adsl, path = paste0(tempdir(), "/adsl.xpt"), + domain = "adsl", metadata = var_spec, strict_checks = FALSE ) diff --git a/tests/testthat/test-format.R b/tests/testthat/test-format.R index 76b65e1d..a311a8c4 100644 --- a/tests/testthat/test-format.R +++ b/tests/testthat/test-format.R @@ -19,3 +19,18 @@ test_that("xportr_format: Gets warning when metadata has multiple rows with same # Checks that message doesn't appear when xportr.domain_name is valid multiple_vars_in_spec_helper2(xportr_format) }) + +test_that("xportr_format: Works as expected with only one domain in metadata", { + adsl <- data.frame( + USUBJID = c(1001, 1002, 1003), + BRTHDT = c(1, 1, 2) + ) + + metadata <- data.frame( + dataset = c("adsl", "adsl"), + variable = c("USUBJID", "BRTHDT"), + format = c(NA, "DATE9.") + ) + + expect_silent(xportr_format(adsl, metadata)) +}) diff --git a/tests/testthat/test-label.R b/tests/testthat/test-label.R index 8030a826..cd0fc30b 100644 --- a/tests/testthat/test-label.R +++ b/tests/testthat/test-label.R @@ -21,3 +21,19 @@ test_that("xportr_label: Gets warning when metadata has multiple rows with same # Checks that message doesn't appear when xportr.domain_name is valid multiple_vars_in_spec_helper2(xportr_label) }) + + +test_that("xportr_label: Works as expected with only one domain in metadata", { + adsl <- data.frame( + USUBJID = c(1001, 1002, 1003), + BRTHDT = c(1, 1, 2) + ) + + metadata <- data.frame( + dataset = c("adsl", "adsl"), + variable = c("USUBJID", "BRTHDT"), + label = c("Hello", "Hello2") + ) + + expect_silent(xportr_label(adsl, metadata)) +}) diff --git a/tests/testthat/test-length.R b/tests/testthat/test-length.R index 35761d84..e3adce3f 100644 --- a/tests/testthat/test-length.R +++ b/tests/testthat/test-length.R @@ -13,7 +13,9 @@ test_that("xportr_length: Accepts valid domain names in metadata object", { withr::local_options(list(xportr.length_verbose = "message")) # Test minimal call with valid data and without domain - xportr_length(adsl, metadata) %>% + adsl %>% + xportr_metadata(domain = "adsl") %>% + xportr_length(metadata) %>% expect_silent() %>% expect_attr_width(metadata$length) @@ -27,7 +29,7 @@ test_that("xportr_length: Accepts valid domain names in metadata object", { # Test minimal call without datasets metadata_without_dataset <- metadata %>% select(-"dataset") - xportr_length(adsl, metadata_without_dataset) %>% + xportr_length(adsl, metadata_without_dataset, domain = "adsl") %>% expect_silent() %>% expect_attr_width(metadata_without_dataset$length) %>% NROW() %>% @@ -59,39 +61,6 @@ test_that("xportr_length: CDISC data frame is being piped after another xportr f expect_equal("adsl") }) -test_that("xportr_length: CDISC data frame domain is being recognized from pipe", { - adsl <- minimal_table(30) - metadata <- minimal_metadata(dataset = TRUE, length = TRUE, var_names = colnames(adsl)) - - # Setup temporary options with `verbose = "message"` - withr::local_options(list(xportr.length_verbose = "message")) - - # Remove empty lines in cli theme - local_cli_theme() - - # With domain manually set - not_adsl <- adsl - result <- not_adsl %>% - xportr_length(metadata) %>% - expect_message("Variable lengths missing from metadata") %>% - expect_message("lengths resolved") %>% - expect_message("Variable\\(s\\) present in dataframe but doesn't exist in `metadata`") - - suppressMessages({ - result <- not_adsl %>% - xportr_length(metadata, verbose = "none") - }) - - expect_no_match(attr(result, "_xportr.df_arg_"), "^adsl$") - - # Test results with piping - result <- adsl %>% - xportr_length(metadata) - - attr(result, "_xportr.df_arg_") %>% - expect_equal("adsl") -}) - test_that("xportr_length: Impute character lengths based on class", { adsl <- minimal_table(30, cols = c("x", "b")) metadata <- minimal_metadata( @@ -109,7 +78,7 @@ test_that("xportr_length: Impute character lengths based on class", { # Test length imputation of character and numeric (not valid character type) result <- adsl %>% - xportr_length(metadata) %>% + xportr_length(metadata, domain = "adsl") %>% expect_silent() expect_attr_width(result, c(7, 199)) @@ -124,7 +93,7 @@ test_that("xportr_length: Impute character lengths based on class", { ) adsl %>% - xportr_length(metadata) %>% + xportr_length(metadata, domain = "adsl") %>% expect_message("Variable lengths missing from metadata") %>% expect_message("lengths resolved") %>% expect_attr_width(c(7, 199, 200, 200, 8)) @@ -140,7 +109,7 @@ test_that("xportr_length: Throws message when variables not present in metadata" local_cli_theme() # Test that message is given which indicates that variable is not present - xportr_length(adsl, metadata) %>% + xportr_length(adsl, metadata, domain = "adsl") %>% expect_message("Variable lengths missing from metadata") %>% expect_message("lengths resolved") %>% expect_message(regexp = "Problem with `y`") @@ -223,3 +192,19 @@ test_that("xportr_length: Gets warning when metadata has multiple rows with same # Checks that message doesn't appear when xportr.domain_name is valid multiple_vars_in_spec_helper2(xportr_length) }) + + +test_that("xportr_length: Works as expected with only one domain in metadata", { + adsl <- data.frame( + USUBJID = c(1001, 1002, 1003), + BRTHDT = c(1, 1, 2) + ) + + metadata <- data.frame( + dataset = c("adsl", "adsl"), + variable = c("USUBJID", "BRTHDT"), + length = c(1, 1) + ) + + expect_silent(xportr_length(adsl, metadata)) +}) diff --git a/tests/testthat/test-metadata.R b/tests/testthat/test-metadata.R index 465d0de5..e50a0741 100644 --- a/tests/testthat/test-metadata.R +++ b/tests/testthat/test-metadata.R @@ -14,7 +14,7 @@ test_that("xportr_label: Correctly applies label from data.frame spec", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame(dataset = "df", variable = c("x", "y"), label = c("foo", "bar")) - df_labeled_df <- xportr_label(df, df_meta) + df_labeled_df <- xportr_label(df, df_meta, domain = "df") expect_equal(extract_var_label(df_labeled_df), c("foo", "bar")) @@ -36,7 +36,7 @@ test_that("xportr_label: Correctly applies label when data is piped", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame(dataset = "df", variable = c("x", "y"), label = c("foo", "bar")) - df_labeled_df <- df %>% xportr_label(df_meta) + df_labeled_df <- df %>% xportr_label(df_meta, domain = "df") expect_equal(extract_var_label(df_labeled_df), c("foo", "bar")) expect_equal( @@ -92,7 +92,7 @@ test_that("xportr_label: Correctly applies label from metacore spec", { )) metacoes_labeled_df <- suppressMessages( - xportr_label(df, metacore_meta) + xportr_label(df, metacore_meta, domain = "df") ) expect_equal(extract_var_label(metacoes_labeled_df), c("X Label", "Y Label", "")) @@ -119,7 +119,7 @@ test_that("xportr_label: Expect error if any variable does not exist in metadata label = "foo" ) suppressMessages( - xportr_label(df, df_meta, verbose = "stop") + xportr_label(df, df_meta, verbose = "stop", domain = "df") ) %>% expect_error() }) @@ -132,7 +132,7 @@ test_that("xportr_label: Expect error if label exceeds 40 characters", { label = strrep("a", 41) ) - suppressMessages(xportr_label(df, df_meta)) %>% + suppressMessages(xportr_label(df, df_meta, domain = "df")) %>% expect_warning("variable label must be 40 characters or less") }) @@ -158,7 +158,7 @@ test_that("xportr_df_label: Correctly applies label from data.frame spec", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame(dataset = "df", label = "Label") - df_spec_labeled_df <- xportr_df_label(df, df_meta) + df_spec_labeled_df <- xportr_df_label(df, df_meta, domain = "df") expect_equal(attr(df_spec_labeled_df, "label"), "Label") expect_equal( @@ -178,6 +178,7 @@ test_that("xportr_df_label: Correctly applies label when data is piped", { df_meta <- data.frame(dataset = "df", label = "Label") df_spec_labeled_df <- df %>% + xportr_metadata(domain = "df") %>% xportr_df_label(df_meta) %>% xportr_df_label(df_meta) @@ -221,7 +222,7 @@ test_that("xportr_df_label: Correctly applies label from metacore spec", { ) )) - metacore_spec_labeled_df <- xportr_df_label(df, metacore_meta) + metacore_spec_labeled_df <- xportr_df_label(df, metacore_meta, domain = "df") expect_equal(attr(metacore_spec_labeled_df, "label"), "Label") expect_equal( @@ -243,7 +244,7 @@ test_that("xportr_df_label: Expect error if label exceeds 40 characters", { ) expect_error( - xportr_df_label(df, df_meta), + xportr_df_label(df, df_meta, domain = "df"), "dataset label must be 40 characters or less" ) }) @@ -273,7 +274,7 @@ test_that("xportr_format: Set formats as expected", { format = c("date9.", "datetime20.") ) - formatted_df <- xportr_format(df, df_meta) + formatted_df <- xportr_format(df, df_meta, domain = "df") expect_equal(extract_format(formatted_df), c("DATE9.", "DATETIME20.")) expect_equal(formatted_df, structure( @@ -293,7 +294,7 @@ test_that("xportr_format: Set formats as expected when data is piped", { format = c("date9.", "datetime20.") ) - formatted_df <- df %>% xportr_format(df_meta) + formatted_df <- df %>% xportr_format(df_meta, domain = "df") expect_equal(extract_format(formatted_df), c("DATE9.", "DATETIME20.")) expect_equal(formatted_df, structure( @@ -321,7 +322,7 @@ test_that("xportr_format: Set formats as expected for metacore spec", { ) )) - formatted_df <- xportr_format(df, metacore_meta) + formatted_df <- xportr_format(df, metacore_meta, domain = "df") expect_equal(extract_format(formatted_df), c("DATE9.", "DATETIME20.")) expect_equal(formatted_df, structure( @@ -361,7 +362,7 @@ test_that("xportr_format: Handle NA values without raising an error", { format = c("date9.", "datetime20.", NA, "text") ) - formatted_df <- xportr_format(df, df_meta) + formatted_df <- xportr_format(df, df_meta, domain = "df") expect_equal(extract_format(formatted_df), c("DATE9.", "DATETIME20.", "", "")) expect_equal(formatted_df, structure( @@ -402,7 +403,7 @@ test_that("xportr_length: Check if width attribute is set properly", { length = c(1, 2) ) - df_with_width <- xportr_length(df, df_meta) + df_with_width <- xportr_length(df, df_meta, domain = "df") expect_equal(c(x = 1, y = 2), map_dbl(df_with_width, attr, "width")) expect_equal(df_with_width, structure( @@ -423,7 +424,7 @@ test_that("xportr_length: Check if width attribute is set properly when data is length = c(1, 2) ) - df_with_width <- df %>% xportr_length(df_meta) + df_with_width <- df %>% xportr_length(df_meta, domain = "df") expect_equal(c(x = 1, y = 2), map_dbl(df_with_width, attr, "width")) expect_equal(df_with_width, structure( @@ -451,7 +452,7 @@ test_that("xportr_length: Check if width attribute is set properly for metacore ) )) - df_with_width <- xportr_length(df, metacore_meta) + df_with_width <- xportr_length(df, metacore_meta, domain = "df") expect_equal(c(x = 1, y = 2), map_dbl(df_with_width, attr, "width")) expect_equal(df_with_width, structure( @@ -494,7 +495,7 @@ test_that("xportr_length: Expect error when a variable is not present in metadat ) suppressMessages( - xportr_length(df, df_meta, verbose = "stop") + xportr_length(df, df_meta, domain = "df", verbose = "stop") ) %>% expect_error("doesn't exist") }) @@ -509,7 +510,7 @@ test_that("xportr_length: Check if length gets imputed when a new variable is pa ) df_with_width <- suppressMessages( - xportr_length(df, df_meta) + xportr_length(df, df_meta, domain = "df") ) # 200 is the imputed length for character and 8 for other data types as in impute_length() @@ -559,68 +560,94 @@ test_that("xportr_metadata: Check metadata interaction with other functions", { rlang::set_names(tolower) expect_equal( - structure(xportr_type(adsl, var_spec), `_xportr.df_metadata_` = var_spec), + structure(xportr_type(adsl, var_spec, domain = "adsl"), `_xportr.df_metadata_` = var_spec), suppressMessages( - xportr_metadata(adsl, var_spec) %>% xportr_type() + xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_type() ) ) expect_equal( structure( - suppressMessages(xportr_length(adsl, var_spec)), + suppressMessages(xportr_length(adsl, var_spec, domain = "adsl")), `_xportr.df_metadata_` = var_spec ), suppressMessages( - xportr_metadata(adsl, var_spec) %>% xportr_length() + xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_length() ) ) expect_equal( structure( - suppressMessages(xportr_label(adsl, var_spec)), + suppressMessages(xportr_label(adsl, var_spec, domain = "adsl")), `_xportr.df_metadata_` = var_spec ), suppressMessages( - xportr_metadata(adsl, var_spec) %>% xportr_label() + xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_label() ) ) expect_equal( structure( - suppressMessages(xportr_order(adsl, var_spec)), + suppressMessages(xportr_order(adsl, var_spec, domain = "adsl")), `_xportr.df_metadata_` = var_spec ), suppressMessages( - xportr_metadata(adsl, var_spec) %>% xportr_order() + xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_order() ) ) expect_equal( structure( - suppressMessages(xportr_format(adsl, var_spec)), + suppressMessages(xportr_format(adsl, var_spec, domain = "adsl")), `_xportr.df_metadata_` = var_spec ), suppressMessages( - xportr_metadata(adsl, var_spec) %>% xportr_format() + xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_format() ) ) }) -test_that("xportr_metadata: Correctly extract domain from var name", { - metadata <- data.frame( - dataset = "adlb", - variable = c("Subj", "Param", "Val", "NotUsed"), - type = c("numeric", "character", "numeric", "character"), - order = c(1, 3, 4, 2) +test_that("xportr_metadata: must throw error if both metadata and domain are null", { + expect_error( + xportr_metadata(data.frame(), metadata = NULL, domain = NULL), + "Must provide either metadata or domain argument" ) +}) + +test_that("xportr_*: Domain is kept in between calls", { + # Divert all messages to tempfile, instead of printing them + # note: be aware as this should only be used in tests that don't track + # messages + withr::local_message_sink(tempfile()) - adlb <- data.frame( - Subj = as.character(123, 456, 789), - Different = c("a", "b", "c"), - Val = c("1", "2", "3"), - Param = c("param1", "param2", "param3") + adsl <- minimal_table(30) + + metadata <- minimal_metadata( + dataset = TRUE, length = TRUE, label = TRUE, type = TRUE, format = TRUE, + order = TRUE ) - expect_equal(attr(xportr_metadata(adlb, metadata), "_xportr.df_arg_"), "adlb") + df2 <- adsl %>% + xportr_metadata(domain = "adsl") %>% + xportr_type(metadata) + + df3 <- df2 %>% + xportr_label(metadata) %>% + xportr_length(metadata) %>% + xportr_order(metadata) %>% + xportr_format(metadata) + + expect_equal(attr(df3, "_xportr.df_arg_"), "adsl") + + df4 <- adsl %>% + xportr_type(metadata, domain = "adsl") + + df5 <- df4 %>% + xportr_label(metadata) %>% + xportr_length(metadata) %>% + xportr_order(metadata) %>% + xportr_format(metadata) + + expect_equal(attr(df5, "_xportr.df_arg_"), "adsl") }) # end diff --git a/tests/testthat/test-order.R b/tests/testthat/test-order.R index 4caba77d..1c68feef 100644 --- a/tests/testthat/test-order.R +++ b/tests/testthat/test-order.R @@ -6,7 +6,7 @@ test_that("xportr_order: Variable are ordered correctly for data.frame spec", { order = 1:4 ) - ordered_df <- suppressMessages(xportr_order(df, df_meta)) + ordered_df <- suppressMessages(xportr_order(df, df_meta, domain = "df")) expect_equal(names(ordered_df), df_meta$variable) }) @@ -21,6 +21,7 @@ test_that("xportr_order: Variable are ordered correctly when data is piped", { ordered_df <- suppressMessages( df %>% + xportr_metadata(domain = "df") %>% xportr_order(df_meta) %>% xportr_order(df_meta) ) @@ -67,7 +68,7 @@ test_that("xportr_order: Variable are ordered correctly for metacore spec", { )) ordered_df <- suppressMessages( - xportr_order(df, metacore_meta) + xportr_order(df, metacore_meta, domain = "df") ) expect_equal(names(ordered_df), ordered_columns) @@ -126,12 +127,12 @@ test_that("xportr_order: Variable ordering messaging is correct", { # Remove empty lines in cli theme local_cli_theme() - xportr_order(df, df_meta, verbose = "message") %>% + xportr_order(df, df_meta, verbose = "message", domain = "df") %>% 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_order(df2, df_meta, verbose = "message") %>% + xportr_order(df2, df_meta, verbose = "message", domain = "df2") %>% 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") @@ -146,7 +147,7 @@ test_that("xportr_order: Metadata order columns are coersed to numeric", { ) ordered_df <- suppressMessages( - xportr_order(df, df_meta) + xportr_order(df, df_meta, domain = "df") ) expect_equal(names(ordered_df), df_meta$variable) @@ -168,3 +169,19 @@ test_that("xportr_order: Gets warning when metadata has multiple rows with same expect_message("All variables in specification file are in dataset") %>% expect_message("All variables in dataset are ordered") }) + + +test_that("xportr_order: Works as expected with only one domain in metadata", { + adsl <- data.frame( + USUBJID = c(1001, 1002, 1003), + BRTHDT = c(1, 1, 2) + ) + + metadata <- data.frame( + dataset = c("adsl", "adsl"), + variable = c("USUBJID", "BRTHDT"), + order = c(1, 2) + ) + + expect_equal(xportr_order(adsl, metadata), adsl) +}) diff --git a/tests/testthat/test-pipe.R b/tests/testthat/test-pipe.R deleted file mode 100644 index c4d18d83..00000000 --- a/tests/testthat/test-pipe.R +++ /dev/null @@ -1,227 +0,0 @@ -test_that("xportr_*: Domain is obtained from a call without pipe", { - adsl <- minimal_table(30) - - metadata <- minimal_metadata( - dataset = TRUE, length = TRUE, label = TRUE, type = TRUE, format = TRUE, - order = TRUE - ) - - # Divert all messages to tempfile, instead of printing them - # note: be aware as this should only be used in tests that don't track - # messages - withr::local_message_sink(tempfile()) - - xportr_metadata(adsl, metadata) %>% - attr("_xportr.df_arg_") %>% - expect_equal("adsl") - xportr_label(adsl, metadata) %>% - attr("_xportr.df_arg_") %>% - expect_equal("adsl") - xportr_length(adsl, metadata) %>% - attr("_xportr.df_arg_") %>% - expect_equal("adsl") - xportr_order(adsl, metadata) %>% - attr("_xportr.df_arg_") %>% - expect_equal("adsl") - xportr_format(adsl, metadata) %>% - attr("_xportr.df_arg_") %>% - expect_equal("adsl") - xportr_type(adsl, metadata) %>% - attr("_xportr.df_arg_") %>% - expect_equal("adsl") -}) - - -test_that("xportr_*: Domain is kept in between calls", { - # Divert all messages to tempfile, instead of printing them - # note: be aware as this should only be used in tests that don't track - # messages - withr::local_message_sink(tempfile()) - - adsl <- minimal_table(30) - - metadata <- minimal_metadata( - dataset = TRUE, length = TRUE, label = TRUE, type = TRUE, format = TRUE, - order = TRUE - ) - - df2 <- adsl %>% - xportr_type(metadata) - - df3 <- df2 %>% - xportr_label(metadata) %>% - xportr_length(metadata) %>% - xportr_order(metadata) %>% - xportr_format(metadata) - - expect_equal(attr(df3, "_xportr.df_arg_"), "adsl") - - df4 <- adsl %>% - xportr_type(metadata) - - df5 <- df4 %>% - xportr_label(metadata) %>% - xportr_length(metadata) %>% - xportr_order(metadata) %>% - xportr_format(metadata) - - expect_equal(attr(df5, "_xportr.df_arg_"), "adsl") -}) - -test_that("xportr_*: Can use magrittr pipe and aquire domain from call", { - # Divert all messages to tempfile, instead of printing them - # note: be aware as this should only be used in tests that don't track - # messages - withr::local_message_sink(tempfile()) - - adsl <- minimal_table(30) - - metadata <- minimal_metadata( - dataset = TRUE, length = TRUE, label = TRUE, type = TRUE, format = TRUE, - order = TRUE - ) - - non_standard_name <- adsl - result <- non_standard_name %>% - xportr_type(metadata) %>% - xportr_label(metadata) %>% - xportr_length(metadata) %>% - xportr_order(metadata) %>% - xportr_format(metadata) %>% - xportr_df_label(metadata) - - expect_equal(attr(result, "_xportr.df_arg_"), "non_standard_name") - - # Different sequence call by moving first and last around - result2 <- non_standard_name %>% - xportr_label(metadata) %>% - xportr_length(metadata) %>% - xportr_order(metadata) %>% - xportr_df_label(metadata) %>% - xportr_type(metadata) %>% - xportr_format(metadata) - - expect_equal(attr(result2, "_xportr.df_arg_"), "non_standard_name") -}) - -test_that("xportr_*: Can use magrittr pipe and aquire domain from call (metadata)", { - # Divert all messages to tempfile, instead of printing them - # note: be aware as this should only be used in tests that don't track - # messages - withr::local_message_sink(tempfile()) - - adsl <- minimal_table(30) - - metadata <- minimal_metadata( - dataset = TRUE, length = TRUE, label = TRUE, type = TRUE, format = TRUE, - order = TRUE - ) - - non_standard_name <- adsl - result <- non_standard_name %>% - xportr_metadata(metadata) %>% - xportr_type() %>% - xportr_label() %>% - xportr_length() %>% - xportr_order() %>% - xportr_format() %>% - xportr_df_label() - - expect_equal(attr(result, "_xportr.df_arg_"), "non_standard_name") - - # Different sequence call by moving first and last around - result2 <- non_standard_name %>% - xportr_metadata(metadata) %>% - xportr_label() %>% - xportr_length() %>% - xportr_order() %>% - xportr_df_label() %>% - xportr_type() %>% - xportr_format() - - expect_equal(attr(result2, "_xportr.df_arg_"), "non_standard_name") -}) - -test_that("xportr_*: Can use R native pipe (R>4.1) and aquire domain from call", { - skip_if( - compareVersion(glue("{R.version$major}.{R.version$minor}"), "4.1.0") < 0, - "R Version doesn't support native pipe (<4.1)" - ) - - # Divert all messages to tempfile, instead of printing them - # note: be aware as this should only be used in tests that don't track - # messages - withr::local_message_sink(tempfile()) - - adsl <- minimal_table(30) - - metadata <- minimal_metadata( - dataset = TRUE, length = TRUE, label = TRUE, type = TRUE, format = TRUE, - order = TRUE - ) - - non_standard_name_native <- adsl - result <- non_standard_name_native |> - xportr_type(metadata) |> - xportr_label(metadata) |> - xportr_length(metadata) |> - xportr_order(metadata) |> - xportr_format(metadata) |> - xportr_df_label(metadata) - - expect_equal(attr(result, "_xportr.df_arg_"), "non_standard_name_native") - - # Different sequence call by moving first and last around - result2 <- non_standard_name_native |> - xportr_label(metadata) |> - xportr_length(metadata) |> - xportr_order(metadata) |> - xportr_df_label(metadata) |> - xportr_type(metadata) |> - xportr_format(metadata) - - expect_equal(attr(result2, "_xportr.df_arg_"), "non_standard_name_native") -}) - -test_that("xportr_*: Can use R native pipe (R>4.1) and aquire domain from call (metadata)", { - skip_if( - compareVersion(glue("{R.version$major}.{R.version$minor}"), "4.1.0") < 0, - "R Version doesn't support native pipe (<4.1)" - ) - - # Divert all messages to tempfile, instead of printing them - # note: be aware as this should only be used in tests that don't track - # messages - withr::local_message_sink(tempfile()) - - adsl <- minimal_table(30) - - metadata <- minimal_metadata( - dataset = TRUE, length = TRUE, label = TRUE, type = TRUE, format = TRUE, - order = TRUE - ) - - non_standard_name_native <- adsl - result <- non_standard_name_native |> - xportr_metadata(metadata) |> - xportr_type() |> - xportr_label() |> - xportr_length() |> - xportr_order() |> - xportr_format() |> - xportr_df_label() - - expect_equal(attr(result, "_xportr.df_arg_"), "non_standard_name_native") - - # Different sequence call by moving first and last around - result2 <- non_standard_name_native |> - xportr_metadata(metadata) |> - xportr_label() |> - xportr_length() |> - xportr_order() |> - xportr_df_label() |> - xportr_type() |> - xportr_format() - - expect_equal(attr(result2, "_xportr.df_arg_"), "non_standard_name_native") -}) diff --git a/tests/testthat/test-type.R b/tests/testthat/test-type.R index c593fdb1..aa31baf1 100644 --- a/tests/testthat/test-type.R +++ b/tests/testthat/test-type.R @@ -29,7 +29,7 @@ test_that("xportr_type: NAs are handled as expected", { ) df2 <- suppressMessages( - xportr_type(df, meta_example) + xportr_type(df, meta_example, domain = "df") ) expect_equal( @@ -52,7 +52,7 @@ test_that("xportr_type: Variable types are coerced as expected and can raise mes # Remove empty lines in cli theme local_cli_theme() - (df2 <- xportr_type(df, meta_example)) %>% + (df2 <- xportr_type(df, meta_example, domain = "df")) %>% expect_message("Variable type mismatches found.") %>% expect_message("[0-9+] variables coerced") @@ -61,9 +61,9 @@ test_that("xportr_type: Variable types are coerced as expected and can raise mes Val = "numeric", Param = "character" )) - expect_error(xportr_type(df, meta_example, verbose = "stop")) + expect_error(xportr_type(df, meta_example, verbose = "stop", domain = "df")) - (df3 <- suppressMessages(xportr_type(df, meta_example, verbose = "warn"))) %>% + (df3 <- suppressMessages(xportr_type(df, meta_example, verbose = "warn", domain = "df"))) %>% expect_warning() expect_equal(purrr::map_chr(df3, class), c( @@ -73,7 +73,7 @@ test_that("xportr_type: Variable types are coerced as expected and can raise mes # Ignore other messages suppressMessages( - (df4 <- xportr_type(df, meta_example, verbose = "message")) %>% + (df4 <- xportr_type(df, meta_example, verbose = "message", domain = "df")) %>% expect_message("Variable type\\(s\\) in dataframe don't match metadata") ) @@ -88,7 +88,7 @@ test_that("xportr_metadata: Var types coerced as expected and raise messages", { local_cli_theme() ( - df2 <- xportr_metadata(df, meta_example) %>% + df2 <- xportr_metadata(df, meta_example, domain = "df") %>% xportr_type() ) %>% expect_message("Variable type mismatches found.") %>% @@ -100,12 +100,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, domain = "df") %>% xportr_type(verbose = "stop") ) %>% expect_error() suppressMessages( - df3 <- xportr_metadata(df, meta_example) %>% xportr_type(verbose = "warn") + df3 <- xportr_metadata(df, meta_example, domain = "df") %>% xportr_type(verbose = "warn") ) %>% expect_warning() @@ -116,7 +116,7 @@ test_that("xportr_metadata: Var types coerced as expected and raise messages", { suppressMessages({ ( - df4 <- xportr_metadata(df, meta_example) %>% + df4 <- xportr_metadata(df, meta_example, domain = "df") %>% xportr_type(verbose = "message") ) %>% expect_message("Variable type\\(s\\) in dataframe don't match metadata: `Subj` and `Val`") @@ -155,12 +155,14 @@ test_that("xportr_type: Variables retain column attributes, besides class", { withr::local_message_sink(tempfile()) df_type_label <- adsl %>% + xportr_metadata(domain = "adsl") %>% xportr_type(metadata) %>% xportr_label(metadata) %>% xportr_length(metadata) %>% xportr_format(metadata) df_label_type <- adsl %>% + xportr_metadata(domain = "adsl") %>% xportr_label(metadata) %>% xportr_length(metadata) %>% xportr_format(metadata) %>% @@ -200,7 +202,7 @@ test_that("xportr_type: works fine from metacore spec", { ) )) processed_df <- suppressMessages( - xportr_type(df, metacore_meta) + xportr_type(df, metacore_meta, domain = "df") ) expect_equal(processed_df$x, "1") }) @@ -213,6 +215,8 @@ test_that("xportr_type: error when metadata is not set", { }) test_that("xportr_type: date variables are not converted to numeric", { + skip_if_not_installed("metacore") + df <- data.frame(RFICDT = as.Date("2017-03-30"), RFICDTM = as.POSIXct("2017-03-30")) metacore_meta <- suppressWarnings( metacore::metacore( @@ -228,7 +232,7 @@ test_that("xportr_type: date variables are not converted to numeric", { ) expect_message( { - processed_df <- xportr_type(df, metacore_meta) + processed_df <- xportr_type(df, metacore_meta, domain = "df") }, NA ) @@ -262,7 +266,7 @@ test_that("xportr_type: date variables are not converted to numeric", { adsl_original$RFICDTM <- as.POSIXct(adsl_original$RFICDTM) expect_message( - adsl_xpt2 <- adsl_original %>% xportr_type(metadata), + adsl_xpt2 <- adsl_original %>% xportr_type(metadata, domain = "adsl_original"), NA ) @@ -300,3 +304,20 @@ test_that("xportr_type: Drops factor levels", { expect_null(attributes(df2$Val)) }) + + +test_that("xportr_type: Works as expected with only one domain in metadata", { + adsl <- data.frame( + USUBJID = c(1001, 1002, 1003), + BRTHDT = c(1, 1, 2) + ) + + metadata <- data.frame( + dataset = c("adsl", "adsl"), + variable = c("USUBJID", "BRTHDT"), + type = c("numeric", "numeric"), + format = c(NA, "DATE9.") + ) + + expect_equal(xportr_type(adsl, metadata), adsl) +}) diff --git a/tests/testthat/test-write.R b/tests/testthat/test-write.R index 4229c06e..44e4718a 100644 --- a/tests/testthat/test-write.R +++ b/tests/testthat/test-write.R @@ -16,7 +16,13 @@ test_that("xportr_write: exported data can still be saved to a file with a label on.exit(unlink(tmpdir)) - suppressWarnings(xportr_write(data_to_save, path = tmp, label = "Lorem ipsum dolor sit amet")) + suppressWarnings( + xportr_write(data_to_save, + path = tmp, + label = "Lorem ipsum dolor sit amet", + domain = "data_to_save" + ) + ) expect_output(str(read_xpt(tmp)), "Lorem ipsum dolor sit amet") }) @@ -29,6 +35,7 @@ test_that("xportr_write: exported data can be saved to a file with a metadata", xportr_write( data_to_save, path = tmp, + domain = "data_to_save", metadata = data.frame( dataset = "data_to_save", label = "Lorem ipsum dolor sit amet" @@ -45,13 +52,14 @@ test_that("xportr_write: exported data can be saved to a file with a existing me df <- xportr_df_label( data_to_save, + domain = "data_to_save", data.frame( dataset = "data_to_save", label = "Lorem ipsum dolor sit amet" ) ) - xportr_write(df, path = tmp) + xportr_write(df, path = tmp, domain = "data_to_save") expect_output(str(read_xpt(tmp)), "Lorem ipsum dolor sit amet") }) @@ -104,6 +112,7 @@ test_that("xportr_write: expect error when label contains non-ASCII symbols or s expect_error( xportr_write( data_to_save, + domain = "data_to_save", tmp, metadata = data.frame( dataset = "data_to_save", @@ -124,6 +133,7 @@ test_that("xportr_write: expect error when label is over 40 characters", { expect_error( xportr_write( data_to_save, + domain = "data_to_save", tmp, metadata = data.frame( dataset = "data_to_save", @@ -143,6 +153,7 @@ test_that("xportr_write: expect error when an xpt validation fails with strict_c expect_error( xportr_write( data_to_save, tmp, + domain = "data_to_save", metadata = data.frame( dataset = "data_to_save", label = "label" @@ -162,6 +173,7 @@ test_that("xportr_write: expect warning when an xpt validation fails with strict expect_warning( xportr_write( data_to_save, tmp, + domain = "data_to_save", metadata = data.frame( dataset = "data_to_save", label = "label" @@ -184,6 +196,7 @@ test_that("xportr_write: Capture errors by haven and report them as such", { suppressWarnings( xportr_write( data_to_save, tmp, + domain = "data_to_save", metadata = data.frame( dataset = "data_to_save", label = "label"