Skip to content

Commit

Permalink
Merge branch 'main' into feature/my_first_fcn
Browse files Browse the repository at this point in the history
  • Loading branch information
bms63 authored Dec 18, 2024
2 parents 1f9937c + ef04a31 commit 6778843
Show file tree
Hide file tree
Showing 57 changed files with 1,179 additions and 487 deletions.
2 changes: 1 addition & 1 deletion .lintr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ linters: linters_with_defaults(
object_usage_linter=NULL,
cyclocomp_linter(complexity_limit = 22),
indentation_linter=NULL,
undesirable_function_linter = undesirable_function_linter()
undesirable_function_linter = undesirable_function_linter(symbol_is_undesirable = FALSE)
)
exclusions: list(
"R/data.R" = Inf,
Expand Down
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: admiral
Title: ADaM in R Asset Library
Version: 1.1.1.9032
Version: 1.1.1.9042
Authors@R: c(
person("Ben", "Straub", , "[email protected]", role = c("aut", "cre")),
person("Stefan", "Bundfuss", role = "aut",
Expand All @@ -17,6 +17,8 @@ Authors@R: c(
person("Sadchla", "Mascary", role = "aut"),
person("Gordon", "Miller", role = "aut"),
person("Lina", "Patil", role = "aut"),
person("Jim", "Rothstein", role = "aut",
comment = c(ORCID = "0009-0009-8659-6071")),
person("Sophie", "Shapcott", role = "aut"),
person("Eric", "Simms", role = "aut"),
person("Daniel", "Sjoberg", role = "aut",
Expand Down Expand Up @@ -44,7 +46,7 @@ BugReports: https://github.com/pharmaverse/admiral/issues
Depends:
R (>= 4.0)
Imports:
admiraldev (>= 1.1.0),
admiraldev (>= 1.1.0.9007),
cli (>= 3.6.2),
dplyr (>= 1.0.5),
hms (>= 0.5.3),
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export(restrict_derivation)
export(set_admiral_options)
export(signal_duplicate_records)
export(slice_derivation)
export(transform_range)
export(use_ad_template)
export(yn_to_numeric)
import(admiraldev)
Expand All @@ -167,6 +168,7 @@ importFrom(cli,cli_text)
importFrom(cli,cli_warn)
importFrom(dplyr,across)
importFrom(dplyr,arrange)
importFrom(dplyr,between)
importFrom(dplyr,bind_cols)
importFrom(dplyr,bind_rows)
importFrom(dplyr,case_when)
Expand All @@ -180,6 +182,7 @@ importFrom(dplyr,first)
importFrom(dplyr,full_join)
importFrom(dplyr,group_by)
importFrom(dplyr,group_by_at)
importFrom(dplyr,group_split)
importFrom(dplyr,if_else)
importFrom(dplyr,lag)
importFrom(dplyr,mutate)
Expand Down Expand Up @@ -256,6 +259,7 @@ importFrom(rlang,call2)
importFrom(rlang,call_name)
importFrom(rlang,caller_env)
importFrom(rlang,cnd_muffle)
importFrom(rlang,cnd_signal)
importFrom(rlang,current_env)
importFrom(rlang,enexpr)
importFrom(rlang,enexprs)
Expand Down Expand Up @@ -319,4 +323,5 @@ importFrom(tidyselect,matches)
importFrom(tidyselect,vars_select)
importFrom(utils,capture.output)
importFrom(utils,file.edit)
importFrom(utils,object.size)
importFrom(utils,str)
14 changes: 13 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
## New Features

- New function `derive_vars_cat()` for deriving pairs of variables or more, e.g.
`AVALCATx` & `AVALCAxN`. (#2480)
`AVALCATy` & `AVALCAyN`. (#2480)
- New function `derive_vars_crit_flag()` for deriving criterion flag variables
(`CRITy`, `CRITyFL`, `CRITyFLN`). (#2468)
- New function `transform_range()` to transform values from a source range to a
target range. (#2571)
- Replace use of `data("sdtm")` with `sdtm <- pharmaverse::sdtm` in templates and vignettes. (#2498)

- Remove `dthcaus_source()` calls in `ADSL` template because they are deprecated. (#2517)
Expand Down Expand Up @@ -39,8 +41,18 @@ or that the queries dataset contains duplicates. (#2543)

- test scripts, R, and markdown files for `create_single_dose_dataset` and `occds.Rmd` updated to include a `STUDYID` column because of `get_admiral_option("subject_keys")` update above. (#2501)

- Update `derive_vars_period()` to make it work when there is only one new variable. (#2582)

- In `get_summary_records()`, previously deprecated formal arguments `analysis_var` and `summary_fun` now removed from function, documentation, tests etc. (#2521)

- A check was added to `derive_vars_transposed()` and `derive_vars_atc()` which
stops execution if the records in `dataset_merge` or `dataset_facm` respectively
are not unique. (#2563)

- The functions `derive_vars_joined()`, `derive_var_joined_exist_flag()`,
`derive_extreme_event()`, and `filter_joined()` were updated to reduce their
memory consumption. (#2590)

## Breaking Changes

- The following function arguments are entering the next phase of the deprecation process: (#2487)
Expand Down
22 changes: 11 additions & 11 deletions R/admiral-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#' @family internal
#' @import admiraldev
#' @importFrom cli cli_abort ansi_collapse cli_div cli_inform cli_text cli_warn
#' @importFrom dplyr across arrange bind_cols bind_rows case_when coalesce
#' desc distinct ends_with everything filter first full_join
#' group_by group_by_at if_else mutate n n_distinct na_if pull
#' rename rename_with row_number select semi_join slice starts_with
#' summarise summarise_all tibble tribble ungroup union lag
#' @importFrom dplyr across arrange between bind_cols bind_rows case_when
#' coalesce desc distinct ends_with everything filter first full_join group_by
#' group_by_at group_split if_else lag mutate n n_distinct na_if pull rename
#' rename_with row_number select semi_join slice starts_with summarise
#' summarise_all tibble tribble ungroup union
#' @importFrom hms as_hms
#' @importFrom lifecycle deprecate_warn deprecate_stop deprecated
#' @importFrom lubridate %--% as_datetime ceiling_date date days duration
Expand All @@ -17,11 +17,11 @@
#' map_if map_lgl map2 modify_at modify_if pmap reduce transpose
#' walk
#' @importFrom rlang := abort arg_match as_data_mask as_function as_label
#' as_name as_string call2 call_name caller_env cnd_muffle current_env .data
#' enexpr enexprs eval_bare eval_tidy expr expr_interp exec expr_label exprs
#' f_lhs f_rhs inform is_call is_expression is_missing is_named list2
#' new_environment new_formula parse_expr parse_exprs set_names sym syms
#' type_of warn
#' as_name as_string call2 call_name caller_env cnd_muffle cnd_signal
#' current_env .data enexpr enexprs eval_bare eval_tidy expr expr_interp exec
#' expr_label exprs f_lhs f_rhs inform is_call is_expression is_missing
#' is_named list2 new_environment new_formula parse_expr parse_exprs set_names
#' sym syms type_of warn
#' @importFrom stats setNames
#' @importFrom stringr str_c str_count str_detect str_extract str_glue
#' str_length str_locate str_locate_all str_match str_remove
Expand All @@ -30,6 +30,6 @@
#' str_trim
#' @importFrom tidyr crossing drop_na fill nest pivot_longer pivot_wider unnest
#' @importFrom tidyselect all_of any_of contains matches vars_select
#' @importFrom utils capture.output file.edit str
#' @importFrom utils capture.output file.edit object.size str
#'
"_PACKAGE"
25 changes: 11 additions & 14 deletions R/compute_scale.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ compute_scale <- function(source,
flip_direction = FALSE,
min_n = 1) {
# Function argument checks
assert_numeric_vector(source) # nolint: undesirable_function_linter
assert_numeric_vector(source_range, optional = TRUE)
assert_numeric_vector(source)
assert_numeric_vector(source_range, length = 2, optional = TRUE)
if (!is.null(target_range) && is.null(source_range)) {
cli_abort(
c("Argument {.arg source_range} is missing with no default
Expand All @@ -84,7 +84,7 @@ compute_scale <- function(source,
)
)
}
assert_numeric_vector(target_range, optional = TRUE)
assert_numeric_vector(target_range, length = 2, optional = TRUE)
if (!is.null(source_range) && is.null(target_range)) {
cli_abort(
c("Argument {.arg target_range} is missing with no default
Expand All @@ -97,19 +97,16 @@ compute_scale <- function(source,
assert_integer_scalar(min_n, subset = "positive")

# Computation
if (sum(!is.na(source)) >= min_n) { # nolint: undesirable_function_linter
target <- mean(source, na.rm = TRUE) # nolint: undesirable_function_linter
if (sum(!is.na(source)) >= min_n) {
target <- mean(source, na.rm = TRUE)

if (!is.null(source_range) && !is.null(target_range)) {
scale_constant <- min(target_range) - min(source_range)
scale_coefficient <- (max(target_range) - min(target_range)) /
(max(source_range) - min(source_range))

target <- (target + scale_constant) * scale_coefficient

if (flip_direction == TRUE) {
target <- max(target_range) - target
}
target <- transform_range(
target,
source_range = source_range,
target_range = target_range,
flip_direction = flip_direction
)
}
} else {
target <- NA
Expand Down
1 change: 0 additions & 1 deletion R/derive_basetype_records.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#' @examples
#' library(tibble)
#' library(dplyr, warn.conflicts = FALSE)
#' library(rlang)
#'
#' bds <- tribble(
#' ~USUBJID, ~EPOCH, ~PARAMCD, ~ASEQ, ~AVAL,
Expand Down
103 changes: 88 additions & 15 deletions R/derive_joined.R
Original file line number Diff line number Diff line change
Expand Up @@ -806,17 +806,92 @@ get_joined_data <- function(dataset,

# join the input dataset with itself such that to each observation of the
# input dataset all following observations are joined
data_add_to_join <- select(
data_add,
!!!by_vars,
!!!replace_values_by_names(extract_vars(order)),
!!!replace_values_by_names(join_vars),
!!tmp_obs_nr_var
)

# split input dataset into smaller pieces and process them separately
# This reduces the memory consumption.
if (is.null(by_vars_left)) {
# create batches of about 1MB input data
obs_per_batch <- floor(1000000 / as.numeric(object.size(data) / nrow(data)))
tmp_batch_nr <- get_new_tmp_var(data, prefix = "tmp_batch_nr")
data_list <- data %>%
mutate(!!tmp_batch_nr := ceiling(row_number() / obs_per_batch)) %>%
group_by(!!tmp_batch_nr) %>%
group_split(.keep = FALSE)
data_add_list <- list(data_add_to_join)
} else {
data_nest <- nest(data, data = everything(), .by = vars2chr(unname(by_vars_left)))
data_add_nest <- nest(data_add, data_add = everything(), .by = vars2chr(unname(by_vars_left)))
data_all_nest <- inner_join(data_nest, data_add_nest, by = vars2chr(by_vars_left))
data_list <- data_all_nest$data
data_add_list <- data_all_nest$data_add
}

joined_data <- map2(
data_list,
data_add_list,
function(x, y) {
get_joined_sub_data(
x,
y,
by_vars = by_vars_left,
tmp_obs_nr_var = tmp_obs_nr_var,
tmp_obs_nr_left = tmp_obs_nr_left,
join_type = join_type,
first_cond_upper = first_cond_upper,
first_cond_lower = first_cond_lower,
filter_join = filter_join
)
}
)

bind_rows(joined_data) %>%
remove_tmp_vars() %>%
select(-!!tmp_obs_nr_var_join)
}

#' Join Data for "joined" functions
#'
#' The helper function joins the data for the "joined" functions. All `.join`
#' variables are included in the output dataset. It is called by
#' `get_joined_data()` to process each by group separately. This reduces the
#' memory consumption.
#'
#' @inheritParams get_joined_data
#'
#' @details
#'
#' 1. The input dataset (`dataset`) and the additional dataset (`dataset_add`)
#' are left joined by the grouping variables (`by_vars`). If no grouping
#' variables are specified, a full join is performed.
#'
#' 1. The joined dataset is restricted as specified by arguments `join_type`,
#' `first_cond_upper`, and `first_cond_lower`. See argument descriptions for
#' details.
#'
#' 1. The joined dataset is restricted by the `filter_join` condition.
#'
#' @keywords internal
get_joined_sub_data <- function(dataset,
dataset_add,
by_vars,
tmp_obs_nr_var,
tmp_obs_nr_left,
join_type,
first_cond_upper,
first_cond_lower,
filter_join) {
data_joined <-
left_join(
data,
select(
data_add,
!!!by_vars,
!!!replace_values_by_names(extract_vars(order)),
!!!replace_values_by_names(join_vars),
!!tmp_obs_nr_var
),
by = vars2chr(by_vars_left),
dataset,
dataset_add,
by = vars2chr(by_vars),
suffix = c("", ".join")
)

Expand All @@ -837,7 +912,7 @@ get_joined_data <- function(dataset,
# select all observations up to the first confirmation observation
data_joined <- filter_relative(
data_joined,
by_vars = expr_c(by_vars_left, tmp_obs_nr_var),
by_vars = expr_c(by_vars, tmp_obs_nr_var),
condition = !!first_cond_upper,
order = exprs(!!parse_expr(paste0(as_name(tmp_obs_nr_var), ".join"))),
mode = "first",
Expand All @@ -851,7 +926,7 @@ get_joined_data <- function(dataset,
# select all observations up to the first confirmation observation
data_joined <- filter_relative(
data_joined,
by_vars = expr_c(by_vars_left, tmp_obs_nr_var),
by_vars = expr_c(by_vars, tmp_obs_nr_var),
condition = !!first_cond_lower,
order = exprs(!!parse_expr(paste0("desc(", as_name(tmp_obs_nr_var), ".join)"))),
mode = "first",
Expand All @@ -862,9 +937,7 @@ get_joined_data <- function(dataset,
}
# apply confirmation condition, which may include summary functions
data_joined %>%
group_by(!!!by_vars_left, !!tmp_obs_nr_left) %>%
group_by(!!!by_vars, !!tmp_obs_nr_left) %>%
filter_if(filter_join) %>%
ungroup() %>%
remove_tmp_vars() %>%
select(-!!tmp_obs_nr_var_join)
ungroup()
}
9 changes: 6 additions & 3 deletions R/derive_param_computed.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
#'
#' Adds a parameter computed from the analysis value of other parameters. It is
#' expected that the analysis value of the new parameter is defined by an
#' expression using the analysis values of other parameters. For example mean
#' arterial pressure (MAP) can be derived from systolic (SYSBP) and diastolic
#' blood pressure (DIABP) with the formula
#' expression using the analysis values of other parameters, such as addition/sum,
#' subtraction/difference, multiplication/product, division/ratio,
#' exponentiation/logarithm, or by formula.
#' <br/><br/>
#' For example mean arterial pressure (MAP) can be derived from systolic (SYSBP)
#' and diastolic blood pressure (DIABP) with the formula
#' \deqn{MAP = \frac{SYSBP + 2 DIABP}{3}}{MAP = (SYSBP + 2 DIABP) / 3}
#'
#' @param dataset
Expand Down
26 changes: 13 additions & 13 deletions R/derive_var_base.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@
#' library(tibble)
#'
#' dataset <- tribble(
#' ~STUDYID, ~USUBJID, ~PARAMCD, ~AVAL, ~AVALC, ~AVISIT, ~ABLFL, ~ANRIND,
#' "TEST01", "PAT01", "PARAM01", 10.12, NA, "Baseline", "Y", "NORMAL",
#' "TEST01", "PAT01", "PARAM01", 9.700, NA, "Day 7", "N", "LOW",
#' "TEST01", "PAT01", "PARAM01", 15.01, NA, "Day 14", "N", "HIGH",
#' "TEST01", "PAT01", "PARAM02", 8.350, NA, "Baseline", "Y", "LOW",
#' "TEST01", "PAT01", "PARAM02", NA, NA, "Day 7", "N", NA,
#' "TEST01", "PAT01", "PARAM02", 8.350, NA, "Day 14", "N", "LOW",
#' "TEST01", "PAT01", "PARAM03", NA, "LOW", "Baseline", "Y", NA,
#' "TEST01", "PAT01", "PARAM03", NA, "LOW", "Day 7", "N", NA,
#' "TEST01", "PAT01", "PARAM03", NA, "MEDIUM", "Day 14", "N", NA,
#' "TEST01", "PAT01", "PARAM04", NA, "HIGH", "Baseline", "Y", NA,
#' "TEST01", "PAT01", "PARAM04", NA, "HIGH", "Day 7", "N", NA,
#' "TEST01", "PAT01", "PARAM04", NA, "MEDIUM", "Day 14", "N", NA
#' ~STUDYID, ~USUBJID, ~PARAMCD, ~AVAL, ~AVALC, ~AVISIT, ~ABLFL, ~ANRIND,
#' "TEST01", "PAT01", "PARAM01", 10.12, NA, "Baseline", "Y", "NORMAL",
#' "TEST01", "PAT01", "PARAM01", 9.700, NA, "Day 7", NA, "LOW",
#' "TEST01", "PAT01", "PARAM01", 15.01, NA, "Day 14", NA, "HIGH",
#' "TEST01", "PAT01", "PARAM02", 8.350, NA, "Baseline", "Y", "LOW",
#' "TEST01", "PAT01", "PARAM02", NA, NA, "Day 7", NA, NA,
#' "TEST01", "PAT01", "PARAM02", 8.350, NA, "Day 14", NA, "LOW",
#' "TEST01", "PAT01", "PARAM03", NA, "LOW", "Baseline", "Y", NA,
#' "TEST01", "PAT01", "PARAM03", NA, "LOW", "Day 7", NA, NA,
#' "TEST01", "PAT01", "PARAM03", NA, "MEDIUM", "Day 14", NA, NA,
#' "TEST01", "PAT01", "PARAM04", NA, "HIGH", "Baseline", "Y", NA,
#' "TEST01", "PAT01", "PARAM04", NA, "HIGH", "Day 7", NA, NA,
#' "TEST01", "PAT01", "PARAM04", NA, "MEDIUM", "Day 14", NA, NA
#' )
#'
#' ## Derive `BASE` variable from `AVAL`
Expand Down
6 changes: 3 additions & 3 deletions R/derive_var_chg.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
#' advs <- tribble(
#' ~USUBJID, ~PARAMCD, ~AVAL, ~ABLFL, ~BASE,
#' "P01", "WEIGHT", 80, "Y", 80,
#' "P01", "WEIGHT", 80.8, "", 80,
#' "P01", "WEIGHT", 81.4, "", 80,
#' "P01", "WEIGHT", 80.8, NA, 80,
#' "P01", "WEIGHT", 81.4, NA, 80,
#' "P02", "WEIGHT", 75.3, "Y", 75.3,
#' "P02", "WEIGHT", 76, "", 75.3
#' "P02", "WEIGHT", 76, NA, 75.3
#' )
#' derive_var_chg(advs)
derive_var_chg <- function(dataset) {
Expand Down
Loading

0 comments on commit 6778843

Please sign in to comment.