From f575bce97ef40e7ab3c9523358f5af5cbe300499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Mon, 26 Feb 2024 19:59:13 +0100 Subject: [PATCH] fix: corrects check errors / notes --- R/length.R | 4 ++-- R/xportr.R | 4 ++++ man/xportr.Rd | 4 ++++ tests/testthat/test-metadata.R | 4 +++- tests/testthat/test-xportr.R | 2 ++ vignettes/deepdive.Rmd | 32 ++++++++++++++++---------------- 6 files changed, 31 insertions(+), 19 deletions(-) diff --git a/R/length.R b/R/length.R index 63da099c..0fac46b3 100644 --- a/R/length.R +++ b/R/length.R @@ -156,8 +156,8 @@ xportr_length <- function(.df, length_df = as.numeric(length_msg[[paste0(variable_length, ".x")]]), length_meta = as.numeric(length_msg[[paste0(variable_length, ".y")]]) ) %>% - filter(length_df < length_meta) %>% - select(all_of(variable_name), length_df, length_meta) + filter(.data$length_df < .data$length_meta) %>% + select(all_of(c(variable_name, "length_df", "length_meta"))) max_length_msg(length_msg, verbose) } diff --git a/R/xportr.R b/R/xportr.R index 003e5cce..367817cb 100644 --- a/R/xportr.R +++ b/R/xportr.R @@ -19,6 +19,10 @@ #' @export #' #' @examplesIf requireNamespace("magrittr") +#' data("adsl_xportr", package = "xportr") +#' data("var_spec", package = "xportr") +#' adsl <- adsl_xportr +#' #' library(magrittr) #' test_dir <- tempdir() #' diff --git a/man/xportr.Rd b/man/xportr.Rd index 7e356559..b5f44fb6 100644 --- a/man/xportr.Rd +++ b/man/xportr.Rd @@ -44,6 +44,10 @@ Wrapper to apply all core xportr functions and write xpt } \examples{ \dontshow{if (requireNamespace("magrittr")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +data("adsl_xportr", package = "xportr") +data("var_spec", package = "xportr") +adsl <- adsl_xportr + library(magrittr) test_dir <- tempdir() diff --git a/tests/testthat/test-metadata.R b/tests/testthat/test-metadata.R index 12f71876..6ed43fa9 100644 --- a/tests/testthat/test-metadata.R +++ b/tests/testthat/test-metadata.R @@ -639,7 +639,7 @@ test_that("xportr_type: Variable types are coerced as expected and can raise mes # start test_that("xportr_metadata: Check metadata interaction with other functions", { # making sure data is being loaded from xportr namespace (name may conflict) - adsl <- xportr::adsl + adsl <- adsl_xportr var_spec <- readxl::read_xlsx( @@ -759,6 +759,8 @@ test_that("xportr_*: Domain is kept in between calls", { # end test_that("`xportr_metadata()` results match traditional results", { + adsl <- adsl_xportr + if (require(magrittr, quietly = TRUE)) { skip_if_not_installed("withr") trad_path <- withr::local_file("adsltrad.xpt") diff --git a/tests/testthat/test-xportr.R b/tests/testthat/test-xportr.R index 60161a72..ee18ab71 100644 --- a/tests/testthat/test-xportr.R +++ b/tests/testthat/test-xportr.R @@ -1,4 +1,6 @@ test_that("pipeline results match `xportr()` results", { + adsl <- adsl_xportr + if (require(magrittr, quietly = TRUE)) { skip_if_not_installed("withr") pipeline_path <- withr::local_file("adslpipe.xpt") diff --git a/vignettes/deepdive.Rmd b/vignettes/deepdive.Rmd index 3e2af697..b8332a1c 100644 --- a/vignettes/deepdive.Rmd +++ b/vignettes/deepdive.Rmd @@ -93,7 +93,7 @@ Each of the core `{xportr}` functions for applying labels, types, formats, order In this section, we are going to explore the 5 core `{xportr}` functions using: -* `xportr::adsl` - An ADSL ADaM dataset from the Pilot 3 Submission to the FDA +* `xportr::adsl_xportr` - An ADSL ADaM dataset from the Pilot 3 Submission to the FDA * `xportr::var_spec` - The ADSL ADaM Specification File from the Pilot 3 Submission to the FDA We will focus on warning and error messaging with contrived examples from these functions by manipulating either the datasets or the specification files. @@ -125,6 +125,7 @@ library(dplyr) library(haven) colnames(var_spec) +data("adsl_xportr", package = "xportr") ``` By using `options()` or `xportr_options()` at the beginning of our script we can tell `{xportr}` what the valid names are (see chunk below). Please note that before we set the options the package assumed every thing was in lowercase and there were no spaces in the names. After running `options()` or `xportr_options()`, `{xportr}` sees the column `Variable` as the valid name rather than `variable`. You can inspect `xportr_options` function docs to look at additional options. @@ -176,7 +177,7 @@ xportr_options( Each of the core `{xportr}` functions requires several inputs: A valid dataframe, a metadata object and a domain name, along with optional messaging. For example, here is a simple call using all of the functions. As you can see, a lot of information is repeated in each call. ```{r, eval = FALSE} -adsl %>% +adsl_xportr %>% xportr_type(var_spec, "ADSL", "message") %>% xportr_length(var_spec, "ADSL", verbose = "message") %>% xportr_label(var_spec, "ADSL", "message") %>% @@ -190,7 +191,7 @@ To help reduce these repetitive calls, we have created `xportr_metadata()`. A us ```{r, eval = FALSE} -adsl %>% +adsl_xportr %>% xportr_metadata(var_spec, "ADSL") %>% xportr_type() %>% xportr_length(length_source = "metadata") %>% @@ -269,7 +270,7 @@ In the `ADSL` data we have several columns that are in the Date type: `TRTSDT` We will change one variable type to a [factor variable](https://forcats.tidyverse.org/), which is a common data structure in R, to give us some educational opportunities to see `xportr_type()` in action. ```{r} -adsl_fct <- adsl %>% +adsl_fct <- adsl_xportr %>% mutate(STUDYID = as_factor(STUDYID)) ``` @@ -308,12 +309,12 @@ adsl_type <- xportr_type(.df = adsl_fct, metadata = var_spec, domain = "ADSL", v Next we will use `xportr_length()` to apply the length column of the _metadata object_ to the `ADSL` dataset. Using the `str()` function we have displayed all the variables with their attributes. You can see that each variable has a label, but there is no information on the lengths of the variable. ```{r, max.height='300px', attr.output='.numberLines', echo = FALSE} -str(adsl) +str(adsl_xportr) ``` ```{r, echo = TRUE} adsl_length <- xportr_length( - .df = adsl, + .df = adsl_xportr, metadata = var_spec, domain = "ADSL", verbose = "warn", @@ -334,7 +335,7 @@ Just like we did for `xportr_type()`, setting `verbose = "stop"` immediately sto ```{r, echo = TRUE, error = TRUE} adsl_length <- xportr_length( - .df = adsl, + .df = adsl_xportr, metadata = var_spec, domain = "ADSL", verbose = "stop", @@ -360,9 +361,9 @@ var_spec_lbl <- var_spec %>% "Length of variable label must be 40 characters or less", label )) -adsl_lbl <- adsl +adsl_lbl <- adsl_xportr -adsl_lbl <- haven::zap_label(adsl) +adsl_lbl <- haven::zap_label(adsl_xportr) ``` We have successfully removed all the labels. @@ -394,7 +395,7 @@ adsl_label <- xportr_label(.df = adsl_lbl, metadata = var_spec_lbl, domain = "AD The order of the dataset can greatly increase readability of the dataset for downstream stakeholders. For example, having all the treatment related variables or analysis variables grouped together can help with inspection and understanding of the dataset. `xportr_order()` can take the order information from the metadata and apply it to your dataset. ```{r} -adsl_ord <- xportr_order(.df = adsl, metadata = var_spec, domain = "ADSL", verbose = "warn") +adsl_ord <- xportr_order(.df = adsl_xportr, metadata = var_spec, domain = "ADSL", verbose = "warn") ``` Readers are encouraged to inspect the dataset and metadata to see the past order and updated order after calling the function. Note the messaging from `xportr_order()`: @@ -404,7 +405,7 @@ Readers are encouraged to inspect the dataset and metadata to see the past order ```{r, echo = TRUE, error = TRUE} -adsl_ord <- xportr_order(.df = adsl, metadata = var_spec, domain = "ADSL", verbose = "stop") +adsl_ord <- xportr_order(.df = adsl_xportr, metadata = var_spec, domain = "ADSL", verbose = "stop") ``` Just like we did for the other functions, setting `verbose = "stop"` immediately stops R from processing the order. If variables or metadata are missing from either, the re-ordering will not process until corrective action is performed. @@ -416,7 +417,7 @@ Formats play an important role in the SAS language and have a column in specific This example is slightly different from previous examples. You will need to use `xportr_type()` to coerce R Date variables and others types to character or numeric. Only then can you use `xportr_format()` to apply the format column to the dataset. ```{r, echo = TRUE} -adsl_fmt <- adsl %>% +adsl_fmt <- adsl_xportr %>% xportr_type(metadata = var_spec, domain = "ADSL", verbose = "warn") %>% xportr_format(metadata = var_spec, domain = "ADSL") ``` @@ -437,7 +438,7 @@ We will make use of `xportr_metadata()` to reduce repetitive metadata and domain It is also note worthy that you can set the dataset label using the `xportr_df_label` and a `dataset_spec` which will be used by the `xportr_write()` ```{r, echo = TRUE, error = TRUE} -adsl %>% +adsl_xportr %>% xportr_metadata(var_spec, "ADSL") %>% xportr_type() %>% xportr_length(length_source = "metadata") %>% @@ -453,7 +454,7 @@ Success! We have applied types, lengths, labels, ordering and formats to our dat The next two examples showcase the `strict_checks = TRUE` option in `xportr_write()` where we will look at formats and labels. ```{r, echo = TRUE, error = TRUE} -adsl %>% +adsl_xportr %>% xportr_write(path = "adsl.xpt", metadata = dataset_spec, domain = "ADSL", strict_checks = TRUE) ``` @@ -468,8 +469,7 @@ var_spec_lbl <- var_spec %>% "Length of variable label must be 40 characters or less", label )) - -adsl %>% +adsl_xportr %>% xportr_metadata(var_spec_lbl, "ADSL") %>% xportr_label() %>% xportr_type() %>%