diff --git a/R/xportr-package.R b/R/xportr-package.R index 197ad5be..be1c7bef 100644 --- a/R/xportr-package.R +++ b/R/xportr-package.R @@ -91,6 +91,7 @@ #' #' #' @keywords internal +#' @aliases xportr-package #' #' @import rlang haven #' @importFrom dplyr left_join bind_cols filter select rename rename_with n diff --git a/R/xportr.R b/R/xportr.R index 687e66d6..b0573c68 100644 --- a/R/xportr.R +++ b/R/xportr.R @@ -20,10 +20,10 @@ #' #' @examples #' -#' has_pkgs <- require(admiral, quietly = TRUE) && -#' require(dplyr, quietly = TRUE) && -#' require(readxl, quietly = TRUE) && -#' require(rlang, quietly = TRUE) +#' has_pkgs <- requireNamespace("admiral", quietly = TRUE) && +#' requireNamespace("dplyr", quietly = TRUE) && +#' requireNamespace("readxl", quietly = TRUE) && +#' requireNamespace("rlang", quietly = TRUE) #' #' if (has_pkgs) { #' adsl <- admiral::admiral_adsl @@ -64,7 +64,7 @@ xportr <- function(.df, path, strict_checks = FALSE) { .df %>% - xportr_metadata(var_metadata, domain, verbose) %>% + xportr_metadata(var_metadata, domain = domain, verbose = verbose) %>% xportr_type() %>% xportr_length() %>% xportr_label() %>% diff --git a/README.Rmd b/README.Rmd index edc532c9..c6a9adbd 100644 --- a/README.Rmd +++ b/README.Rmd @@ -143,9 +143,9 @@ adsl %>% The `xportr_metadata()` function can reduce duplication by setting the variable specification and domain explicitly at the top of a pipeline. If you would like to use the `verbose` argument, you will need to set in each function call. -```{r, message=FALSE, eval=FALSE} +```{r, warning=FALSE, message=FALSE, eval=FALSE} adsl %>% - xportr_metadata(var_spec, "ADSL", verbose = "warn") %>% + xportr_metadata(var_spec, domain = "ADSL") %>% xportr_type() %>% xportr_length() %>% xportr_label() %>% diff --git a/README.md b/README.md index cc83fae4..592fcce1 100644 --- a/README.md +++ b/README.md @@ -155,7 +155,7 @@ each function call. ``` r adsl %>% - xportr_metadata(var_spec, "ADSL") %>% + xportr_metadata(var_spec, domain = "ADSL") %>% xportr_type() %>% xportr_length() %>% xportr_label() %>% diff --git a/man/xportr-package.Rd b/man/xportr-package.Rd index 64eaed80..ab8f471e 100644 --- a/man/xportr-package.Rd +++ b/man/xportr-package.Rd @@ -2,8 +2,8 @@ % Please edit documentation in R/xportr-package.R \docType{package} \name{xportr-package} -\alias{xportr} \alias{xportr-package} +\alias{_PACKAGE} \title{The \code{xportr} package} \description{ \code{xportr} is designed to be a clinical workflow friendly method for outputting diff --git a/tests/testthat/test-metadata.R b/tests/testthat/test-metadata.R index 27b80500..97fd0e79 100644 --- a/tests/testthat/test-metadata.R +++ b/tests/testthat/test-metadata.R @@ -594,13 +594,13 @@ test_that("xportr_metadata: Variable ordering messaging is correct", { ) # Metadata versions - xportr_metadata(df, df_meta, verbose = "message") %>% + xportr_metadata(df, df_meta, domain = "df", 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_metadata(df2, df_meta, domain = "df2", 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`") %>% @@ -623,13 +623,13 @@ test_that("xportr_type: Variable types are coerced as expected and can raise mes # Metadata version of the last statement df %>% - xportr_metadata(meta_example, verbose = "warn") %>% + xportr_metadata(meta_example, domain = "df", verbose = "warn") %>% xportr_type() %>% expect_warning() # Metadata version df %>% - xportr_metadata(meta_example, verbose = "message") %>% + xportr_metadata(meta_example, domain = "df", verbose = "message") %>% xportr_type() %>% expect_message("Variable type\\(s\\) in dataframe don't match metadata") }) @@ -654,7 +654,8 @@ test_that("xportr_metadata: Check metadata interaction with other functions", { `_xportr.df_metadata_` = var_spec, `_xportr.df_verbose_` = "none"), suppressMessages( - xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_type() + xportr_metadata(adsl, var_spec, domain = "adsl", verbose = "none") %>% + xportr_type() ) ) @@ -663,7 +664,8 @@ test_that("xportr_metadata: Check metadata interaction with other functions", { `_xportr.df_metadata_` = var_spec, `_xportr.df_verbose_` = "none"), suppressMessages( - xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_length() + xportr_metadata(adsl, var_spec, domain = "adsl", verbose = "none") %>% + xportr_length() ) ) @@ -672,7 +674,8 @@ test_that("xportr_metadata: Check metadata interaction with other functions", { `_xportr.df_metadata_` = var_spec, `_xportr.df_verbose_` = "none"), suppressMessages( - xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_label() + xportr_metadata(adsl, var_spec, domain = "adsl", verbose = "none") %>% + xportr_label() ) ) @@ -681,7 +684,8 @@ test_that("xportr_metadata: Check metadata interaction with other functions", { `_xportr.df_metadata_` = var_spec, `_xportr.df_verbose_` = "none"), suppressMessages( - xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_order() + xportr_metadata(adsl, var_spec, domain = "adsl", verbose = "none") %>% + xportr_order() ) ) @@ -690,7 +694,8 @@ test_that("xportr_metadata: Check metadata interaction with other functions", { `_xportr.df_metadata_` = var_spec, `_xportr.df_verbose_` = "none"), suppressMessages( - xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_format() + xportr_metadata(adsl, var_spec, domain = "adsl", verbose = "none") %>% + xportr_format() ) ) }) diff --git a/tests/testthat/test-xportr.R b/tests/testthat/test-xportr.R new file mode 100644 index 00000000..6ebb129c --- /dev/null +++ b/tests/testthat/test-xportr.R @@ -0,0 +1,52 @@ +test_that("pipeline results match `xportr()` results", { + + has_pkgs <- requireNamespace("admiral", quietly = TRUE) && + requireNamespace("dplyr", quietly = TRUE) && + requireNamespace("readxl", quietly = TRUE) && + requireNamespace("rlang", quietly = TRUE) + + if (has_pkgs) { + adsl <- admiral::admiral_adsl + + spec_path <- system.file(paste0("specs/", "ADaM_admiral_spec.xlsx"), + package = "xportr") + + var_spec <- readxl::read_xlsx(spec_path, sheet = "Variables") %>% + dplyr::rename(type = "Data Type") %>% + rlang::set_names(tolower) + dataset_spec <- readxl::read_xlsx(spec_path, sheet = "Datasets") %>% + dplyr::rename(label = "Description") %>% + rlang::set_names(tolower) + + test_dir <- tempdir() + + pipeline_path <- file.path(test_dir, "adslpipe.xpt") + xportr_path <- file.path(test_dir, "adslxptr.xpt") + + pipeline_df <- adsl %>% + xportr_metadata(var_spec, "ADSL", verbose = "none") %>% + xportr_type() %>% + xportr_length() %>% + xportr_label() %>% + xportr_order() %>% + xportr_format() %>% + xportr_df_label(dataset_spec) %>% + xportr_write(pipeline_path) + + # `xportr()` can be used to apply a whole pipeline at once + xportr_df <- xportr(adsl, + var_metadata = var_spec, + df_metadata = dataset_spec, + domain = "ADSL", + verbose = "none", + path = xportr_path + ) + + expect_identical(pipeline_df, xportr_df) + + expect_identical( + haven::read_xpt(pipeline_path), + haven::read_xpt(xportr_path) + ) + } +})