atorus-research / xportr

Tools to build CDISC compliant data sets and check for CDISC compliance.
https://atorus-research.github.io/xportr/
Other
41 stars 8 forks source link

Bug: explicitly filter domain variables using `.data` and `.env` pronouns #174

Closed ynsec37 closed 6 months ago

ynsec37 commented 1 year ago

What happened?

When using the xportr to write the full sdtm(parent sdtm and supplemental in one dataset), it may be not a submission dataset but very usefull. I hope it can provide more flexibility, although the xportr is designed to created CDISC compliant submission datasets.

In this case I need create a variable that contains both parent variables and supplemental variables, use another variable to distinguish them.

After checking the final output, the supplemental variables are ignored, e.g. missing label.

This is due to the filtering code metadata <- metadata %>% filter(!!sym(domain_name)==domain) will turn to metadata <- metadata %>% filter(dataset == domain), then it will filter the records dataset == domain not dataset == "AE"

For now, I just delete the domain when writing xpt. The more robust way is to explicitly use the .data and .env to avoid it like below metadata <- metadata %>% filter(.data[[domain_name]]==.env$domain)

Session Information

R version 4.2.3 (2023-03-15 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale: [1] LC_COLLATE=Chinese (Simplified)_China.utf8 LC_CTYPE=Chinese (Simplified)_China.utf8 LC_MONETARY=Chinese (Simplified)_China.utf8 [4] LC_NUMERIC=C LC_TIME=Chinese (Simplified)_China.utf8

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] dplyr_1.1.2 xportr_0.3.0 devtools_2.4.5 usethis_2.2.2

loaded via a namespace (and not attached): [1] Rcpp_1.0.11 lubridate_1.9.2 prettyunits_1.1.1 ps_1.7.5 digest_0.6.33 utf8_1.2.3 mime_0.12
[8] slam_0.1-50 R6_2.5.1 reprex_2.0.2 evaluate_0.21 pillar_1.9.0 rlang_1.1.1 rstudioapi_0.15.0 [15] miniUI_0.1.1.1 callr_3.7.3 urlchecker_1.0.1 R.utils_2.12.2 R.oo_1.25.0 rmarkdown_2.23 styler_1.10.1
[22] readr_2.1.4 stringr_1.5.0 htmlwidgets_1.6.2 shiny_1.7.4.1 xfun_0.39 compiler_4.2.3 httpuv_1.6.11
[29] janitor_2.2.0 pkgconfig_2.0.3 pkgbuild_1.4.2 dso.sdtm_0.1.0 clipr_0.8.0 htmltools_0.5.5 tidyselect_1.2.0 [36] tibble_3.2.1 fansi_1.0.4 crayon_1.5.2 tzdb_0.4.0 withr_2.5.0 later_1.3.1 R.methodsS3_1.8.2 [43] brio_1.1.3 xtable_1.8-4 lifecycle_1.0.3 magrittr_2.0.3 cli_3.6.1 stringi_1.7.12 cachem_1.0.8
[50] fs_1.6.2 promises_1.2.0.1 remotes_2.4.2 testthat_3.1.10 NLP_0.2-1 snakecase_0.11.0 xml2_1.3.5
[57] ellipsis_0.3.2 generics_0.1.3 vctrs_0.6.3 tools_4.2.3 forcats_1.0.0 R.cache_0.16.0 glue_1.6.2
[64] purrr_1.0.1 hms_1.1.3 yaml_2.3.7 processx_3.8.2 pkgload_1.3.2.1 parallel_4.2.3 fastmap_1.1.1
[71] timechange_0.2.0 datapasta_3.1.0 tm_0.7-11 sessioninfo_1.2.2 memoise_2.0.1 knitr_1.43 haven_2.5.3
[78] profvis_0.3.8

Reproducible Example

library(dplyr,warn.conflicts = FALSE)
library(xportr)

ae_full <- tibble::tribble(
  ~STUDYID, ~DOMAIN,      ~USUBJID,                     ~AETERM, ~AETRTEM,
  "CDISCPILOT01",    "AE", "01-701-1015", "APPLICATION SITE ERYTHEMA", "Y"
)

ae_meta <- tibble::tribble(
  ~domain, ~dataset, ~variable,       ~type,                                ~label, ~order, ~length, ~format,
  "AE",     "AE", "STUDYID", "character",                    "Study Identifier",     1L,    "15",      NA,
  "AE",     "AE",  "DOMAIN", "character",                 "Domain Abbreviation",     2L,     "2",      NA,
  "AE",     "AE", "USUBJID", "character",           "Unique Subject Identifier",     3L,    "25",      NA,
  "AE",     "AE",  "AETERM", "character", "Reported Term for the Adverse Event",     4L,   "200",      NA,
  "SUPPAE",     "AE", "AETRTEM", "character",             "Treatment Emergent Flag",    5L,    "1",      NA
)

ae_full %>%
  xportr_metadata(ae_meta, "AE") %>%
  xportr_type() %>%
  xportr_length() %>%
  xportr_label() %>%
  xportr_order() %>%
  xportr_format() %>% 
  str()
#> 
#> ── Variable lengths missing from metadata. ──
#> 
#> ✔ 1 lengths resolved
#> 
#> ── Variable labels missing from metadata. ──
#> 
#> ✔ 1 labels skipped
#> 
#> ── 1 variables not in spec and moved to end ──
#> 
#> ── All variables in dataset are ordered ──
#> 
#> tibble [1 × 5] (S3: tbl_df/tbl/data.frame)
#>  $ STUDYID: chr "CDISCPILOT01"
#>   ..- attr(*, "width")= chr "15"
#>   ..- attr(*, "label")= chr "Study Identifier"
#>   ..- attr(*, "format.sas")= chr ""
#>  $ DOMAIN : chr "AE"
#>   ..- attr(*, "width")= chr "2"
#>   ..- attr(*, "label")= chr "Domain Abbreviation"
#>   ..- attr(*, "format.sas")= chr ""
#>  $ USUBJID: chr "01-701-1015"
#>   ..- attr(*, "width")= chr "25"
#>   ..- attr(*, "label")= chr "Unique Subject Identifier"
#>   ..- attr(*, "format.sas")= chr ""
#>  $ AETERM : chr "APPLICATION SITE ERYTHEMA"
#>   ..- attr(*, "width")= chr "200"
#>   ..- attr(*, "label")= chr "Reported Term for the Adverse Event"
#>   ..- attr(*, "format.sas")= chr ""
#>  $ AETRTEM: chr "Y"
#>   ..- attr(*, "width")= num 200
#>   ..- attr(*, "label")= chr ""
#>   ..- attr(*, "format.sas")= chr ""
#>  - attr(*, "_xportr.df_arg_")= chr "AE"
#>  - attr(*, "_xportr.df_metadata_")= tibble [5 × 8] (S3: tbl_df/tbl/data.frame)
#>   ..$ domain  : chr [1:5] "AE" "AE" "AE" "AE" ...
#>   ..$ dataset : chr [1:5] "AE" "AE" "AE" "AE" ...
#>   ..$ variable: chr [1:5] "STUDYID" "DOMAIN" "USUBJID" "AETERM" ...
#>   ..$ type    : chr [1:5] "character" "character" "character" "character" ...
#>   ..$ label   : chr [1:5] "Study Identifier" "Domain Abbreviation" "Unique Subject Identifier" "Reported Term for the Adverse Event" ...
#>   ..$ order   : int [1:5] 1 2 3 4 5
#>   ..$ length  : chr [1:5] "15" "2" "25" "200" ...
#>   ..$ format  : logi [1:5] NA NA NA NA NA

Created on 2023-07-27 with reprex v2.0.2

EeethB commented 6 months ago

@ynsec37 this is fixed now in #256. If you are able to test it out, I'd love to hear if it's working for you!

ynsec37 commented 6 months ago

@ynsec37 this is fixed now in #256. If you are able to test it out, I'd love to hear if it's working for you!

@EeethB Thank you very much. I had tested it and fixed the above case. image