diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7cc68495..2b570ee1 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,7 +6,7 @@ default_language_version: python: python3 repos: - repo: https://github.com/lorenzwalthert/precommit - rev: v0.4.0 + rev: v0.4.2 hooks: - id: style-files name: Style code with `styler` diff --git a/DESCRIPTION b/DESCRIPTION index ee259c4b..01a3975a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: random.cdisc.data Title: Create Random ADaM Datasets -Version: 0.3.15.9000 -Date: 2024-03-28 +Version: 0.3.15.9002 +Date: 2024-04-16 Authors@R: c( person("Pawel", "Rucki", , "pawel.rucki@roche.com", role = "aut"), person("Nick", "Paszty", , "npaszty@gene.com", role = "aut"), @@ -19,6 +19,10 @@ Description: A set of functions to create random Analysis Data Model described by the Clinical Data Interchange Standards Consortium (CDISC) Analysis Data Model Team. License: Apache License 2.0 +URL: https://insightsengineering.github.io/random.cdisc.data/, + https://github.com/insightsengineering/random.cdisc.data/ +BugReports: + https://github.com/insightsengineering/random.cdisc.data/issues Depends: R (>= 3.6) Imports: diff --git a/NAMESPACE b/NAMESPACE index cc332219..518c5375 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -28,7 +28,6 @@ export(replace_na) export(rtexp) export(rtpois) export(sample_fct) -export(var_relabel) export(visit_schedule) import(dplyr) importFrom(lifecycle,badge) diff --git a/NEWS.md b/NEWS.md index ae188ce4..bd1da54a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,8 @@ -# random.cdisc.data 0.3.15.9000 +# random.cdisc.data 0.3.15.9002 + +### Miscellaneous +* Renamed `var_relabel` into `rcd_var_relabel` and copied new functionalities to avoid namespace conflict with `formatters` package. +* Made `rcd_var_relabel` internal as it was intended for developers use only. # random.cdisc.data 0.3.15 diff --git a/R/radae.R b/R/radae.R index 358777e6..0c32e5cb 100644 --- a/R/radae.R +++ b/R/radae.R @@ -134,7 +134,7 @@ radae <- function(adsl, AETOXGR %in% c(4, 5) ~ "SEVERE" )) - adae <- var_relabel( + adae <- rcd_var_relabel( adae, STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" diff --git a/R/radaette.R b/R/radaette.R index 5a6e165e..599918b2 100644 --- a/R/radaette.R +++ b/R/radaette.R @@ -223,12 +223,12 @@ radaette <- function(adsl, dplyr::bind_rows(patient_data, ae_data) }) %>% Reduce(rbind, .) %>% - var_relabel( + rcd_var_relabel( STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" ) - adaette <- var_relabel( + adaette <- rcd_var_relabel( adaette, STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" diff --git a/R/radcm.R b/R/radcm.R index 4953129d..f804f8e6 100644 --- a/R/radcm.R +++ b/R/radcm.R @@ -82,7 +82,7 @@ radcm <- function(adsl, `[`(c(4, 5, 1, 2, 3)) %>% dplyr::mutate(CMCAT = CMCLAS) - adcm <- var_relabel( + adcm <- rcd_var_relabel( adcm, STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" diff --git a/R/raddv.R b/R/raddv.R index 11ad35fe..d3e7b581 100644 --- a/R/raddv.R +++ b/R/raddv.R @@ -101,7 +101,7 @@ raddv <- function(adsl, Reduce(rbind, .) %>% dplyr::mutate(DVSCAT = DVCAT) - addv <- var_relabel( + addv <- rcd_var_relabel( addv, STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" diff --git a/R/radeg.R b/R/radeg.R index 9870f90c..b395a4ac 100644 --- a/R/radeg.R +++ b/R/radeg.R @@ -167,16 +167,12 @@ radeg <- function(adsl, dplyr::mutate(BNRIND = ANRIND[ABLFL == "Y"]) %>% dplyr::ungroup() %>% dplyr::mutate(ATPTN = 1) %>% - dplyr::mutate(DTYPE = NA) %>% - var_relabel( - STUDYID = attr(adeg$STUDYID, "label"), - USUBJID = attr(adeg$USUBJID, "label") - ) + dplyr::mutate(DTYPE = NA) adeg$ANRIND <- factor(adeg$ANRIND, levels = c("LOW", "NORMAL", "HIGH")) adeg$BNRIND <- factor(adeg$BNRIND, levels = c("LOW", "NORMAL", "HIGH")) - adeg <- var_relabel( + adeg <- rcd_var_relabel( adeg, STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" diff --git a/R/radex.R b/R/radex.R index fb5ac101..643162b3 100644 --- a/R/radex.R +++ b/R/radex.R @@ -213,7 +213,7 @@ radex <- function(adsl, AVAL )) - adex <- var_relabel( + adex <- rcd_var_relabel( adex, STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" diff --git a/R/radhy.R b/R/radhy.R index fa5d5b98..25009b38 100644 --- a/R/radhy.R +++ b/R/radhy.R @@ -168,7 +168,7 @@ radhy <- function(adsl, dplyr::ungroup() adhy <- adhy %>% - var_relabel( + rcd_var_relabel( STUDYID = attr(adsl$STUDYID, "label"), USUBJID = attr(adsl$USUBJID, "label") ) diff --git a/R/radlb.R b/R/radlb.R index 03533aa6..19e50823 100644 --- a/R/radlb.R +++ b/R/radlb.R @@ -260,7 +260,7 @@ radlb <- function(adsl, ATOXGR == "-3" ~ "", ATOXGR == "-4" ~ "", ))) %>% - var_relabel( + rcd_var_relabel( STUDYID = attr(adsl$STUDYID, "label"), USUBJID = attr(adsl$USUBJID, "label") ) @@ -294,7 +294,7 @@ radlb <- function(adsl, # merge grade_lookup onto adlb adlb <- dplyr::left_join(adlb, grade_lookup, by = "PARAMCD") - adlb <- var_relabel( + adlb <- rcd_var_relabel( adlb, STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" diff --git a/R/radmh.R b/R/radmh.R index eb3bb1a8..ed5eafc5 100644 --- a/R/radmh.R +++ b/R/radmh.R @@ -81,7 +81,7 @@ radmh <- function(adsl, `[`(c(4, 5, 1, 2, 3)) %>% dplyr::mutate(MHTERM = MHDECOD) - admh <- var_relabel( + admh <- rcd_var_relabel( admh, STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" diff --git a/R/radqlqc.R b/R/radqlqc.R index 459191fd..7dc93978 100644 --- a/R/radqlqc.R +++ b/R/radqlqc.R @@ -111,14 +111,14 @@ radqlqc <- function(adsl, dplyr::mutate(PCHG2 = 100 * (CHG2 / BASE2)) %>% dplyr::mutate(CHG = AVAL - BASE) %>% dplyr::mutate(PCHG = 100 * (CHG / BASE)) %>% - var_relabel( + rcd_var_relabel( STUDYID = attr(adsl$STUDYID, "label"), USUBJID = attr(adsl$USUBJID, "label") ) # derive CHGCAT1 ---------------------------------------------------------- adqlqc_tmp <- derv_chgcat1(dataset = adqlqc_tmp) - adqlqc_tmp <- var_relabel( + adqlqc_tmp <- rcd_var_relabel( adqlqc_tmp, STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" diff --git a/R/radqs.R b/R/radqs.R index b242297b..5ff2a55a 100644 --- a/R/radqs.R +++ b/R/radqs.R @@ -128,12 +128,12 @@ radqs <- function(adsl, dplyr::mutate(PCHG2 = 100 * (CHG2 / BASE2)) %>% dplyr::mutate(CHG = AVAL - BASE) %>% dplyr::mutate(PCHG = 100 * (CHG / BASE)) %>% - var_relabel( + rcd_var_relabel( STUDYID = attr(adsl$STUDYID, "label"), USUBJID = attr(adsl$USUBJID, "label") ) - adqs <- var_relabel( + adqs <- rcd_var_relabel( adqs, STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" diff --git a/R/radrs.R b/R/radrs.R index 6807eb94..7f670766 100644 --- a/R/radrs.R +++ b/R/radrs.R @@ -145,12 +145,12 @@ radrs <- function(adsl, }) %>% Reduce(rbind, .) %>% dplyr::mutate(AVALC = factor(AVALC, levels = names(param_codes))) %>% - var_relabel( + rcd_var_relabel( STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" ) - adrs <- var_relabel( + adrs <- rcd_var_relabel( adrs, STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" diff --git a/R/radsub.R b/R/radsub.R index 5a76de2e..5d28d89a 100644 --- a/R/radsub.R +++ b/R/radsub.R @@ -143,7 +143,7 @@ radsub <- function(adsl, adsub <- adsub[order(adsub$STUDYID, adsub$USUBJID, adsub$PARAMCD), ] - adsub <- var_relabel( + adsub <- rcd_var_relabel( adsub, STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" diff --git a/R/radtte.R b/R/radtte.R index 8f0ef978..34bae219 100644 --- a/R/radtte.R +++ b/R/radtte.R @@ -105,7 +105,7 @@ radtte <- function(adsl, dplyr::select(-"LAMBDA", -"CNSR_P") }) %>% Reduce(rbind, .) %>% - var_relabel( + rcd_var_relabel( STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" # ) ) @@ -202,7 +202,7 @@ radtte <- function(adsl, adtte <- do.call("rbind", adtte_lst) rownames(adtte) <- NULL - adtte <- var_relabel( + adtte <- rcd_var_relabel( adtte, STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" diff --git a/R/radvs.R b/R/radvs.R index af638c27..d0e5349b 100644 --- a/R/radvs.R +++ b/R/radvs.R @@ -184,12 +184,12 @@ radvs <- function(adsl, dplyr::ungroup() %>% dplyr::mutate(ATPTN = 1) %>% dplyr::mutate(DTYPE = NA) %>% - var_relabel( + rcd_var_relabel( USUBJID = attr(adsl$USUBJID, "label"), STUDYID = attr(adsl$STUDYID, "label") ) - advs <- var_relabel( + advs <- rcd_var_relabel( advs, STUDYID = "Study Identifier", USUBJID = "Unique Subject Identifier" diff --git a/R/utils.R b/R/utils.R index 3d1d12f7..53ad1ac4 100644 --- a/R/utils.R +++ b/R/utils.R @@ -180,28 +180,32 @@ retain <- function(df, value_var, event, outside = NA) { #' Primary Keys: Labels #' -#' Relabel a subset of variables in a data set. +#' @description Shallow copy of `formatters::var_relabel()`. Used mainly internally to +#' relabel a subset of variables in a data set. #' #' @param x (`data.frame`)\cr Data frame containing variables to which labels are applied. #' @param ... (`named character`)\cr Name-Value pairs, where name corresponds to a variable -#' name in `x` and the value to the new variable label. +#' name in `x` and the value to the new variable label. #' @return x (`data.frame`)\cr Data frame with labels applied. #' -#' @export -#' -#' @examples -#' adsl <- radsl() -#' var_relabel(adsl, -#' STUDYID = "Study Identifier", -#' USUBJID = "Unique Subject Identifier" -#' ) -var_relabel <- function(x, ...) { +#' @keywords internal +rcd_var_relabel <- function(x, ...) { + stopifnot(is.data.frame(x)) + if (missing(...)) { + return(x) + } dots <- list(...) varnames <- names(dots) if (is.null(varnames)) { stop("missing variable declarations") } - map_varnames <- match(varnames, names(x)) + map_varnames <- match(varnames, colnames(x)) + if (any(is.na(map_varnames))) { + stop("variables: ", paste(varnames[is.na(map_varnames)], collapse = ", "), " not found") + } + if (any(vapply(dots, Negate(is.character), logical(1)))) { + stop("all variable labels must be of type character") + } for (i in seq_along(map_varnames)) { attr(x[[map_varnames[[i]]]], "label") <- dots[[i]] } diff --git a/_pkgdown.yml b/_pkgdown.yml index f190d5d9..dcefcfc3 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -96,5 +96,5 @@ reference: - rtexp - rtpois - sample_fct - - var_relabel + - rcd_var_relabel - visit_schedule diff --git a/man/random.cdisc.data-package.Rd b/man/random.cdisc.data-package.Rd index 6c047719..dfa683f3 100644 --- a/man/random.cdisc.data-package.Rd +++ b/man/random.cdisc.data-package.Rd @@ -7,6 +7,15 @@ \title{\code{random.cdisc.data} Package} \description{ Package to create random SDTM and ADAM datasets. +} +\seealso{ +Useful links: +\itemize{ + \item \url{https://insightsengineering.github.io/random.cdisc.data/} + \item \url{https://github.com/insightsengineering/random.cdisc.data/} + \item Report bugs at \url{https://github.com/insightsengineering/random.cdisc.data/issues} +} + } \author{ \strong{Maintainer}: Joe Zhu \email{joe.zhu@roche.com} diff --git a/man/var_relabel.Rd b/man/rcd_var_relabel.Rd similarity index 67% rename from man/var_relabel.Rd rename to man/rcd_var_relabel.Rd index f9a32366..c28c9f54 100644 --- a/man/var_relabel.Rd +++ b/man/rcd_var_relabel.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/utils.R -\name{var_relabel} -\alias{var_relabel} +\name{rcd_var_relabel} +\alias{rcd_var_relabel} \title{Primary Keys: Labels} \usage{ -var_relabel(x, ...) +rcd_var_relabel(x, ...) } \arguments{ \item{x}{(\code{data.frame})\cr Data frame containing variables to which labels are applied.} @@ -16,12 +16,7 @@ name in \code{x} and the value to the new variable label.} x (\code{data.frame})\cr Data frame with labels applied. } \description{ -Relabel a subset of variables in a data set. -} -\examples{ -adsl <- radsl() -var_relabel(adsl, - STUDYID = "Study Identifier", - USUBJID = "Unique Subject Identifier" -) +Shallow copy of \code{formatters::var_relabel()}. Used mainly internally to +relabel a subset of variables in a data set. } +\keyword{internal} diff --git a/random.cdisc.data.Rproj b/random.cdisc.data.Rproj index 08804b79..270314b8 100644 --- a/random.cdisc.data.Rproj +++ b/random.cdisc.data.Rproj @@ -16,5 +16,6 @@ AutoAppendNewline: Yes StripTrailingWhitespace: Yes BuildType: Package +PackageUseDevtools: Yes PackageInstallArgs: --no-multiarch --with-keep.source PackageRoxygenize: rd,collate,namespace diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 7735737e..99777999 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -79,17 +79,17 @@ test_that("retain works as expected", { expect_equal(result, expected) }) -test_that("var_relabel works as expected", { - result <- random.cdisc.data:::var_relabel( +test_that("rcd_var_relabel works as expected", { + result <- rcd_var_relabel( data.frame(letters = letters[1:5]), letters = "Letter Variable" ) expect_identical(attr(result$letters, "label"), "Letter Variable") }) -test_that("var_relabel returns correct error message", { +test_that("rcd_var_relabel returns correct error message", { expect_error( - random.cdisc.data:::var_relabel( + rcd_var_relabel( data.frame(letters = letters[1:5]), "Letter Variable" ), "missing variable declarations"