Skip to content

Commit

Permalink
Closes #237 Removes admiral dependency and corrects site (#240)
Browse files Browse the repository at this point in the history
* feat: remove admiral from dependencies

* fix: remove redundant parameters

* update ADSL dataset

* fix: updates metadata and vignettes

* chore: lint and styler

* docs: updates news

* docs: Reframe adsl data source

* replace rlang::set_names with dplyr::rename_with

* docs: remove extra note

* docs: simplify example

* revert: bring back abort instead of stop

* fix: remove rlang from vignettes

* chore: explicit use of rlang and haven namespace

* feat: rename adsl to adsl_xportr and ADaM_admiral_spec.xlsx to ADaM_spec.xlsx

* docs: update NAMESPACE

* fix: corrects check errors / notes

* feat: data is non-lazy load and not easily accessible to user

* docs: update spec file

* docs: update news

* fix: updates example after merge

* fix: linter problem

---------

Co-authored-by: Ben Straub <ben.x.straub@gsk.com>
  • Loading branch information
averissimo and bms63 authored Feb 27, 2024
1 parent bb9e7c5 commit b479b71
Show file tree
Hide file tree
Showing 30 changed files with 290 additions and 274 deletions.
2 changes: 0 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ Imports:
stringr (>= 1.4.0),
tidyselect
Suggests:
admiral,
DT,
knitr,
labelled,
Expand All @@ -51,6 +50,5 @@ VignetteBuilder:
knitr
Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
12 changes: 10 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export(xportr_split)
export(xportr_type)
export(xportr_write)
export(xpt_validate)
import(haven)
import(rlang)
importFrom(checkmate,assert)
importFrom(checkmate,assert_character)
importFrom(checkmate,assert_choice)
Expand Down Expand Up @@ -56,6 +54,7 @@ importFrom(dplyr,ungroup)
importFrom(glue,glue)
importFrom(glue,glue_collapse)
importFrom(graphics,stem)
importFrom(haven,write_xpt)
importFrom(lifecycle,deprecated)
importFrom(magrittr,"%>%")
importFrom(magrittr,extract2)
Expand All @@ -68,6 +67,15 @@ importFrom(purrr,pluck)
importFrom(purrr,walk)
importFrom(purrr,walk2)
importFrom(readr,parse_number)
importFrom(rlang,"%||%")
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(rlang,abort)
importFrom(rlang,inform)
importFrom(rlang,local_options)
importFrom(rlang,sym)
importFrom(rlang,warn)
importFrom(rlang,with_options)
importFrom(stringr,str_detect)
importFrom(stringr,str_extract)
importFrom(stringr,str_replace)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ done to make the use of xportr functions more explicit. (#182)
* 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`.

* `SASlength` and `SAStype` were removed since they did not have an impact on `xpt_validate` or any other functions (#132)
* Removes `admiral` from suggested dependencies (#237)
* `adsl` data object is now called `adsl_xportr` (#237)
* Data objects are no longer lazy loaded, which means that when needed the user must call `data("name_of_object")` first (#237)

## Documentation

Expand Down
91 changes: 50 additions & 41 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,70 @@
#'
#' An example dataset containing subject level data
#'
#' @format ## `adsl`
#' A data frame with 254 rows and 48 columns:
#' @source Dataset created by `admiral::use_ad_template("adsl")`
#' @usage data("adsl_xportr")
#'
#' @format ## `adsl_xportr`
#' A data frame with 306 rows and 51 columns:
#' \describe{
#' \item{STUDYID}{Study Identifier}
#' \item{USUBJID}{Unique Subject Identifier}
#' \item{SUBJID}{Subject Identifier for the Study}
#' \item{RFSTDTC}{Subject Reference Start Date/Time}
#' \item{RFENDTC}{Subject Reference End Date/Time}
#' \item{RFXSTDTC}{Date/Time of First Study Treatment}
#' \item{RFXENDTC}{Date/Time of Last Study Treatment}
#' \item{RFICDTC}{Date/Time of Informed Consent}
#' \item{RFPENDTC}{Date/Time of End of Participation}
#' \item{DTHDTC}{Date/Time of Death}
#' \item{DTHFL}{Subject Death Flag}
#' \item{SITEID}{Study Site Identifier}
#' \item{SITEGR1}{Pooled Site Group 1}
#' \item{AGE}{Age}
#' \item{AGEU}{Age Units}
#' \item{SEX}{Sex}
#' \item{RACE}{Race}
#' \item{ETHNIC}{Ethnicity}
#' \item{ARMCD}{Planned Arm Code}
#' \item{ARM}{Description of Planned Arm}
#' \item{ACTARMCD}{Actual Arm Code}
#' \item{ACTARM}{Description of Actual Arm}
#' \item{COUNTRY}{Country}
#' \item{DMDTC}{Date/Time of Collection}
#' \item{DMDY}{Study Day of Collection}
#' \item{TRT01P}{Planned Treatment for Period 01}
#' \item{TRT01PN}{Planned Treatment for Period 01 (N)}
#' \item{TRT01A}{Actual Treatment for Period 01}
#' \item{TRT01AN}{Actual Treatment for Period 01 (N)}
#' \item{TRTSDTM}{Datetime of First Exposure to Treatment}
#' \item{TRTSTMF}{Time of First Exposure Imputation Flag}
#' \item{TRTEDTM}{Datetime of Last Exposure to Treatment}
#' \item{TRTETMF}{Time of Last Exposure Imputation Flag}
#' \item{TRTSDT}{Date of First Exposure to Treatment}
#' \item{TRTEDT}{Date of Last Exposure to Treatment}
#' \item{TRTDUR}{Duration of Treatment (days)}
#' \item{AVGDD}{Avg Daily Dose (as planned)}
#' \item{CUMDOSE}{Cumulative Dose (as planned)}
#' \item{AGE}{Age}
#' \item{AGEGR1}{Pooled Age Group 1}
#' \item{AGEGR1N}{Pooled Age Group 1 (N)}
#' \item{AGEU}{Age Units}
#' \item{RACE}{Race}
#' \item{RACEN}{Race (N)}
#' \item{SEX}{Sex}
#' \item{ETHNIC}{Ethnicity}
#' \item{TRTDURD}{Total Treatment Duration (Days)}
#' \item{SCRFDT}{Screen Failure Date}
#' \item{EOSDT}{End of Study Date}
#' \item{EOSSTT}{End of Study Status}
#' \item{FRVDT}{Final Retrieval Visit Date}
#' \item{RANDDT}{Date of Randomization}
#' \item{DTHDT}{Date of Death}
#' \item{DTHDTF}{Date of Death Imputation Flag}
#' \item{DTHADY}{Relative Day of Death}
#' \item{LDDTHELD}{Elapsed Days from Last Dose to Death}
#' \item{LSTALVDT}{Date Last Known Alive}
#' \item{SAFFL}{Safety Population Flag}
#' \item{ITTFL}{Intent-To-Treat Population Flag}
#' \item{EFFFL}{Efficacy Population Flag}
#' \item{COMP8FL}{Completers of Week 8 Population Flag}
#' \item{COMP16FL}{Completers of Week 16 Population Flag}
#' \item{COMP24FL}{Completers of Week 24 Population Flag}
#' \item{DISCONFL}{Did the Subject Discontinue the Study}
#' \item{DSRAEFL}{Discontinued due to AE}
#' \item{DTHFL}{Subject Died}
#' \item{BMIBL}{Baseline BMI (kg/m^2)}
#' \item{BMIBLGR1}{Pooled Baseline BMI Group 1}
#' \item{HEIGHTBL}{Baseline Height (cm)}
#' \item{WEIGHTBL}{Baseline Weight (kg)}
#' \item{EDUCLVL}{Years of Education}
#' \item{DISONSDT}{Date of Onset of Disease}
#' \item{DURDIS}{Duration of Disease (Months)}
#' \item{DURDSGR1}{Pooled Disease Duration Group 1}
#' \item{VISIT1DT}{Date of Visit 1}
#' \item{RFSTDTC}{Subject Reference Start Date/Time}
#' \item{RFENDTC}{Subject Reference End Date/Time}
#' \item{VISNUMEN}{End of Trt Visit (Vis 12 or Early Term.)}
#' \item{RFENDT}{Date of Discontinuation/Completion}
#' \item{DCDECOD}{Standardized Disposition Term}
#' \item{DCREASCD}{Reason for Discontinuation}
#' \item{MMSETOT}{MMSE Total}
#' \item{RACEGR1}{Pooled Race Group 1}
#' \item{AGEGR1}{Pooled Age Group 1}
#' \item{REGION1}{Geographic Region 1}
#' \item{LDDTHGR1}{Last Dose to Death - Days Elapsed Group 1}
#' \item{DTH30FL}{Death Within 30 Days of Last Trt Flag}
#' \item{DTHA30FL}{Death After 30 Days from Last Trt Flag}
#' \item{DTHB30FL}{Death Within 30 Days of First Trt Flag}
#' }
"adsl"
"adsl_xportr"

#' Example Dataset Variable Specification
#'
#' @usage data("var_spec")
#'
#' @format ## `var_spec`
#' A data frame with 216 rows and 19 columns:
#' \describe{
Expand Down Expand Up @@ -85,6 +93,7 @@

#' Example Dataset Specification
#'
#' @usage data("dataset_spec")
#' @format ## `dataset_spec`
#' A data frame with 1 row and 9 columns:
#' \describe{
Expand Down
9 changes: 2 additions & 7 deletions R/length.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
#' @inheritParams xportr
#' @param metadata A data frame containing variable level metadata. See
#' 'Metadata' section for details.
#' @param domain Appropriate CDISC 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.
#' @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'
#' @param length_source Choose the assigned length from either metadata or data.
#'
#' If `"metadata"` is specified, the assigned length is from the metadata length.
Expand Down Expand Up @@ -155,13 +149,14 @@ xportr_length <- function(.df,
attr(.df[[i]], "width") <- length_data[[i]]
}


length_msg <- left_join(var_length_max, metadata[, c(variable_name, variable_length)], by = variable_name)
length_msg <- length_msg %>%
mutate(
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) %>%
filter(.data$length_df < .data$length_meta) %>%
select(any_of(c(variable_name, "length_df", "length_meta")))

max_length_msg(length_msg, verbose)
Expand Down
12 changes: 5 additions & 7 deletions R/metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@
#'
#' xportr_metadata(adlb, metadata, "test")
#'
#' if (rlang::is_installed("magrittr")) {
#' library(magrittr)
#' library(magrittr)
#'
#' adlb %>%
#' xportr_metadata(metadata, "test") %>%
#' xportr_type() %>%
#' xportr_order()
#' }
#' adlb %>%
#' xportr_metadata(metadata, "test") %>%
#' xportr_type() %>%
#' xportr_order()
xportr_metadata <- function(.df,
metadata = NULL,
domain = NULL,
Expand Down
2 changes: 2 additions & 0 deletions R/split.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#' @export
#'
#' @examples
#' data("adsl_xportr")
#' adsl <- adsl_xportr
#'
#' adlb <- data.frame(
#' USUBJID = c(1001, 1002, 1003),
Expand Down
2 changes: 1 addition & 1 deletion R/support-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ minimal_table <- function(n_rows = 3, cols = c("x", "y")) {
d = sample(Sys.Date() + c(1, -1, 10, -10), size = n_rows, replace = TRUE),
e = sample(c(1, 2), replace = TRUE, size = n_rows)
) %>%
mutate(e = if_else(seq_along(e) %% 2 == 0, NA, e)) %>%
mutate(e = if_else(seq_along(.data$e) %% 2 == 0, NA, .data$e)) %>%
select(all_of(tolower(cols)))
}

Expand Down
2 changes: 1 addition & 1 deletion R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ xportr_write <- function(.df,
}
},
error = function(err) {
rlang::abort(
abort(
paste0(
"Error reported by haven::write_xpt, error was: \n",
err
Expand Down
4 changes: 3 additions & 1 deletion R/xportr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@
#' @keywords internal
#' @aliases xportr-package
#'
#' @import rlang haven
#' @importFrom haven write_xpt
#' @importFrom rlang abort warn inform with_options local_options .data := sym
#' %||%
#' @importFrom dplyr left_join bind_cols filter select rename rename_with n
#' everything arrange group_by summarize mutate ungroup case_when distinct
#' tribble if_else across as_tibble
Expand Down
3 changes: 3 additions & 0 deletions R/xportr.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#' @export
#'
#' @examplesIf requireNamespace("magrittr")
#' data("adsl_xportr", "dataset_spec", "var_spec")
#' adsl <- adsl_xportr
#'
#' library(magrittr)
#' test_dir <- tempdir()
#'
Expand Down
21 changes: 11 additions & 10 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -104,33 +104,34 @@ To do this we will need to do the following:

All of which can be done using a well-defined specification file and the `{xportr}` package!

First we will start with our `ADSL` dataset created in R. This example `ADSL` dataset is taken from the [`{admiral}`](https://pharmaverse.github.io/admiral/index.html) package. The script that generates this `ADSL` dataset can be created by using this command `admiral::use_ad_template("adsl")`. This `ADSL` dataset has 306 observations and 48 variables.
First we will start with our `ADSL` dataset created in R.
This example `ADSL` dataset contains 306 observations and 51 variables.

```{r, eval=TRUE, message=FALSE, warning=FALSE}
library(dplyr)
library(admiral)
library(xportr)
adsl <- admiral::admiral_adsl
data("adsl_xportr")
ADSL <- adsl_xportr
```

We have created a dummy specification file called `ADaM_admiral_spec.xlsx` found in the `specs` folder of this package. You can use `system.file(paste0("specs/", "ADaM_admiral_spec.xlsx"), package = "xportr")` to access this file.
We have created a dummy specification file called `ADaM_spec.xlsx` found in the `specs` folder of this package. You can use `system.file(file.path("specs/", "ADaM_spec.xlsx"), package = "xportr")` to access this file.

```{r}
spec_path <- system.file(paste0("specs/", "ADaM_admiral_spec.xlsx"), package = "xportr")
spec_path <- system.file(file.path("specs", "ADaM_spec.xlsx"), package = "xportr")
var_spec <- readxl::read_xlsx(spec_path, sheet = "Variables") %>%
dplyr::rename(type = "Data Type") %>%
rlang::set_names(tolower)
dplyr::rename_with(tolower)
dataset_spec <- readxl::read_xlsx(spec_path, sheet = "Datasets") %>%
dplyr::rename(label = "Description") %>%
rlang::set_names(tolower)
dplyr::rename_with(tolower)
```

Each `xportr_` function has been written in a way to take in a part of the specification file and apply that piece to the dataset. Setting `verbose = "warn"` will send appropriate warning message to the console. We have suppressed the warning for the sake of brevity.

```{r, warning = FALSE, message=FALSE, eval=TRUE}
adsl %>%
ADSL %>%
xportr_metadata(var_spec, "ADSL") %>%
xportr_type(verbose = "warn") %>%
xportr_length(verbose = "warn") %>%
Expand All @@ -144,7 +145,7 @@ 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, warning=FALSE, message=FALSE, eval=FALSE}
adsl %>%
ADSL %>%
xportr_metadata(var_spec, "ADSL", verbose = "warn") %>%
xportr_type() %>%
xportr_length() %>%
Expand All @@ -159,7 +160,7 @@ Furthermore, if you're calling all xportr functions at once with common metadata

```{r, warning=FALSE, message=FALSE, eval=FALSE}
xportr(
.df = adsl,
.df = ADSL,
var_metadata = var_spec,
df_metadata = dataset_spec,
domain = "ADSL",
Expand Down
Loading

0 comments on commit b479b71

Please sign in to comment.