Skip to content

Commit

Permalink
Merge branch 'devel' into 1940_reevaluate_package_runtime_dependencie…
Browse files Browse the repository at this point in the history
…s@devel
  • Loading branch information
bms63 authored Aug 2, 2023
2 parents beb00a8 + b6f39a5 commit be901c4
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 82 deletions.
4 changes: 2 additions & 2 deletions .Rprofile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Set renv profile base on R version.
renv_profile <- paste(R.version$major, substr(R.version$minor, 1, 1), sep = ".")
if (file.exists("./renv/profile")) {
message("Using renv profile from `renv/profile` file.")
message("Using renv profile from `renv/profile` file.")
} else if (renv_profile %in% c("4.1", "4.2", "4.3")) {
message("Set renv profile to `", renv_profile, "`")
Sys.setenv("RENV_PROFILE" = renv_profile)
Expand All @@ -11,6 +11,6 @@ if (file.exists("./renv/profile")) {

if ((Sys.getenv("GITHUB_ACTIONS") != "") || (Sys.getenv("DOCKER_CONTAINER_CONTEXT") != "")) {
options(repos = c(CRAN = "https://cran.rstudio.com"))
Sys.setenv(RENV_AUTOLOADER_ENABLED=FALSE)
Sys.setenv(RENV_AUTOLOADER_ENABLED = FALSE)
}
source("renv/activate.R")
2 changes: 1 addition & 1 deletion .github/workflows/cran-status.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
with:
# Whom should the issue be assigned to if errors are encountered
# in the CRAN status checks?
issue-assignees: "bundfussr,esimms999-gsk,thomas-neitmann,bms63"
issue-assignees: "bundfussr,esimms999-gsk,manciniedoardo,bms63"
# Create an issue if one or more of the following
# statuses are reported on the check report.
statuses: "WARN,ERROR,NOTE"
Expand Down
11 changes: 11 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

- The function `derive_var_extreme_flag()` has a new function argument, `flag_all` that additionally flags all records if the first or last record is not unique. (#1979)

- The function `derive_vars_dy()` is updated to avoid potential error when the input `dataset` with columns ending with `temp`. (#2012)


## Breaking Changes
- The following functions, which were deprecated in previous `{admiral}` versions, have been removed: (#1950)

Expand All @@ -34,13 +37,21 @@
- The `filter` argument in `derive_extreme_records()` was deprecated in favor of
the `filter_add` using the next phase of the deprecation process. (#1950)

- The `na_val` argument in `derive_var_shift()` has been deprecated in favor of
`missing_value` using the first phase of the deprecation process. (#2014)

- The `dataset_expected_obs` argument in `derive_expected_records()` and `derive_locf_records()`
has been deprecated in favor of `dataset_ref`. (#2037)

## Documentation


## Various

- The list of package authors/contributors has been reformatted so that those who are actively maintaining the code base are now marked as *authors*, whereas those who made a significant contribution in the past are now down as *contributors*. All other acknowledgements have been moved to README section (#1941).

- `derive_vars_joined()` had two bugs with regards to duplicates messaging and when `new_vars` was set to `NULL` that have now been addressed (#1966).

# admiral 0.11.1

- Fix bug in `derive_param_tte()`. (#1962)
Expand Down
37 changes: 25 additions & 12 deletions R/derive_expected_records.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
#'
#' @param dataset Input dataset
#'
#' A data frame, the columns from `dataset_expected_obs` and specified by the
#' A data frame, the columns from `dataset_ref` and specified by the
#' `by_vars` parameter are expected.
#'
#' @param dataset_expected_obs Expected observations dataset
#' @param dataset_expected_obs *Deprecated*, please use `dataset_ref` instead.
#'
#' @param dataset_ref Expected observations dataset
#'
#' Data frame with the expected observations, e.g., all the expected
#' combinations of `PARAMCD`, `PARAM`, `AVISIT`, `AVISITN`, ...
#'
#' @param by_vars Grouping variables
#'
#' For each group defined by `by_vars` those observations from `dataset_expected_obs`
#' For each group defined by `by_vars` those observations from `dataset_ref`
#' are added to the output dataset which do not have a corresponding observation
#' in the input dataset.
#'
Expand All @@ -31,7 +33,7 @@
#' "TDOSE", PARCAT1 = "OVERALL")`.
#'
#' @details For each group (the variables specified in the `by_vars` parameter),
#' those records from `dataset_expected_obs` that are missing in the input
#' those records from `dataset_ref` that are missing in the input
#' dataset are added to the output dataset.
#'
#' @return The input dataset with the missed expected observations added for each
Expand Down Expand Up @@ -63,7 +65,7 @@
#'
#' derive_expected_records(
#' dataset = adqs,
#' dataset_expected_obs = parm_visit_ref,
#' dataset_ref = parm_visit_ref,
#' by_vars = exprs(USUBJID, PARAMCD),
#' set_values_to = exprs(DTYPE = "DERIVED")
#' )
Expand All @@ -78,35 +80,46 @@
#'
#' derive_expected_records(
#' dataset = adqs,
#' dataset_expected_obs = parm_visit_ref,
#' dataset_ref = parm_visit_ref,
#' by_vars = exprs(USUBJID, PARAMCD),
#' set_values_to = exprs(DTYPE = "DERIVED")
#' )
#'
derive_expected_records <- function(dataset,
dataset_expected_obs,
dataset_ref,
by_vars = NULL,
set_values_to = NULL) {
if (!missing(dataset_expected_obs)) {
deprecate_warn(
"0.12.0",
"derive_expected_records(dataset_expected_obs = )",
"derive_expected_records(dataset_ref = )"
)
assert_data_frame(dataset_expected_obs)
dataset_ref <- dataset_expected_obs
}

# Check input parameters
assert_vars(by_vars, optional = TRUE)
assert_data_frame(dataset_expected_obs)
assert_data_frame(dataset_ref)
assert_data_frame(
dataset,
required_vars = expr_c(by_vars, chr2vars(colnames(dataset_expected_obs)))
required_vars = expr_c(by_vars, chr2vars(colnames(dataset_ref)))
)
assert_varval_list(set_values_to, optional = TRUE)

# Derive expected records
## ids: Variables from by_vars but not in dataset_expected_obs
## ids: Variables from by_vars but not in dataset_ref
ids <- dataset %>%
select(!!!setdiff(by_vars, chr2vars(colnames(dataset_expected_obs)))) %>%
select(!!!setdiff(by_vars, chr2vars(colnames(dataset_ref)))) %>%
distinct()

if (ncol(ids) > 0) {
exp_obsv <- ids %>%
crossing(dataset_expected_obs)
crossing(dataset_ref)
} else {
exp_obsv <- dataset_expected_obs
exp_obsv <- dataset_ref
} # tmp workaround, update after using tidyr 1.2.0

exp_obs_vars <- exp_obsv %>%
Expand Down
16 changes: 15 additions & 1 deletion R/derive_joined.R
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,19 @@ derive_vars_joined <- function(dataset,
if (is.null(new_vars)) {
new_vars <- chr2vars(colnames(dataset_add))
}
preexisting_vars <- chr2vars(colnames(dataset))
preexisting_vars_no_by_vars <- preexisting_vars[which(!(preexisting_vars %in% by_vars))]
duplicates <- intersect(replace_values_by_names(new_vars), preexisting_vars_no_by_vars)
if (length(duplicates) > 0) {
err_msg <- sprintf(
paste(
"The following columns in `dataset_add` have naming conflicts with `dataset`,\n",
"please make the appropriate modifications to `new_vars`, with respect to:\n%s"
),
enumerate(vars2chr(duplicates))
)
abort(err_msg)
}

# number observations of the input dataset to get a unique key
# (by_vars and tmp_obs_nr)
Expand All @@ -371,7 +384,7 @@ derive_vars_joined <- function(dataset,
filter_if(filter_add) %>%
select(
!!!by_vars,
!!!chr2vars(names(order)),
!!!replace_values_by_names(extract_vars(order)),
!!!replace_values_by_names(join_vars),
!!!intersect(unname(extract_vars(new_vars)), chr2vars(colnames(dataset_add)))
)
Expand Down Expand Up @@ -410,6 +423,7 @@ derive_vars_joined <- function(dataset,
by_vars = exprs(!!!by_vars_left, !!tmp_obs_nr),
new_vars = add_suffix_to_vars(new_vars, vars = common_vars, suffix = ".join"),
missing_values = missing_values,
check_type = check_type,
duplicate_msg = paste(
paste(
"After applying `filter_join` the joined dataset contains more",
Expand Down
34 changes: 24 additions & 10 deletions R/derive_locf_records.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
#' The columns specified by the `by_vars`, `analysis_var`, `order`,
#' `keep_vars` parameters are expected.
#'
#' @param dataset_expected_obs Expected observations dataset
#' @param dataset_expected_obs *Deprecated*, please use `dataset_ref` instead.
#'
#' @param dataset_ref Expected observations dataset
#'
#' Data frame with all the combinations of `PARAMCD`, `PARAM`, `AVISIT`,
#' `AVISITN`, ... which are expected in the dataset is expected.
#'
#'
#' @param by_vars Grouping variables
#'
#' For each group defined by `by_vars` those observations from `dataset_expected_obs`
#' For each group defined by `by_vars` those observations from `dataset_ref`
#' are added to the output dataset which do not have a corresponding observation
#' in the input dataset or for which `analysis_var` is `NA` for the corresponding observation
#' in the input dataset.
Expand All @@ -40,7 +43,7 @@
#' @author G Gayatri
#'
#' @details For each group (with respect to the variables specified for the
#' by_vars parameter) those observations from dataset_expected_obs are added to
#' by_vars parameter) those observations from `dataset_ref` are added to
#' the output dataset
#' - which do not have a corresponding observation in the input dataset or
#' - for which `analysis_var` is NA for the corresponding observation in the input dataset.
Expand Down Expand Up @@ -101,19 +104,30 @@
#' )
#'
#' derive_locf_records(
#' data = advs,
#' dataset_expected_obs = advs_expected_obsv,
#' dataset = advs,
#' dataset_ref = advs_expected_obsv,
#' by_vars = exprs(STUDYID, USUBJID, PARAMCD),
#' order = exprs(AVISITN, AVISIT),
#' keep_vars = exprs(PARAMN)
#' )
#'
derive_locf_records <- function(dataset,
dataset_expected_obs,
dataset_ref,
by_vars,
analysis_var = AVAL,
order,
keep_vars = NULL) {
if (!missing(dataset_expected_obs)) {
deprecate_warn(
"0.12.0",
"derive_locf_records(dataset_expected_obs = )",
"derive_locf_records(dataset_ref = )"
)
assert_data_frame(dataset_expected_obs)
dataset_ref <- dataset_expected_obs
}

#### Input Checking ####
analysis_var <- assert_symbol(enexpr(analysis_var))

Expand All @@ -123,27 +137,27 @@ derive_locf_records <- function(dataset,
assert_expr_list(order)

# Check by_vars and order variables in input datasets
assert_data_frame(dataset_expected_obs)
assert_data_frame(dataset_ref)
assert_data_frame(
dataset,
required_vars = expr_c(
by_vars, analysis_var, extract_vars(order), keep_vars,
chr2vars(colnames(dataset_expected_obs))
chr2vars(colnames(dataset_ref))
)
)


#### Prepping 'dataset_expected_obs' ####
#### Prepping 'dataset_ref' ####


# Get the IDs from input dataset for which the expected observations are to be added

ids <- dataset %>%
select(!!!setdiff(by_vars, chr2vars(colnames(dataset_expected_obs)))) %>%
select(!!!setdiff(by_vars, chr2vars(colnames(dataset_ref)))) %>%
distinct()

exp_obsv <- ids %>%
crossing(dataset_expected_obs)
crossing(dataset_ref)



Expand Down
22 changes: 16 additions & 6 deletions R/derive_var_shift.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#'
#' @param to_var Variable containing value to shift to.
#'
#' @param na_val Character string to replace missing values in `from_var` or `to_var`.
#' @param na_val *Deprecated*, please use `missing_value` instead.
#'
#' @param missing_value Character string to replace missing values in `from_var` or `to_var`.
#'
#' Default: "NULL"
#'
Expand All @@ -24,7 +26,7 @@
#'
#' @details `new_var` is derived by concatenating the values of `from_var` to values of `to_var`
#' (e.g. "NORMAL to HIGH"). When `from_var` or `to_var` has missing value, the
#' missing value is replaced by `na_val` (e.g. "NORMAL to NULL").
#' missing value is replaced by `missing_value` (e.g. "NORMAL to NULL").
#'
#'
#' @return The input dataset with the character shift variable added
Expand Down Expand Up @@ -71,20 +73,28 @@ derive_var_shift <- function(dataset,
new_var,
from_var,
to_var,
na_val = "NULL",
na_val,
missing_value = "NULL",
sep_val = " to ") {
### BEGIN DEPRECATION
if (!missing(na_val)) {
deprecate_warn("0.12.0", "derive_var_shift(na_val = )", "derive_var_shift(missing_value = )")
missing_value <- na_val
}
### END DEPRECATION

new_var <- assert_symbol(enexpr(new_var))
from_var <- assert_symbol(enexpr(from_var))
to_var <- assert_symbol(enexpr(to_var))
na_val <- assert_character_scalar(na_val)
missing_value <- assert_character_scalar(missing_value)
sep_val <- assert_character_scalar(sep_val)
assert_data_frame(dataset, required_vars = exprs(!!from_var, !!to_var))

# Derive shift variable. If from_var or to_var has missing value then set to na_val.
dataset %>%
mutate(
temp_from_var = if_else(is.na(!!from_var), !!na_val, as.character(!!from_var)),
temp_to_var = if_else(is.na(!!to_var), !!na_val, as.character(!!to_var))
temp_from_var = if_else(is.na(!!from_var), !!missing_value, as.character(!!from_var)),
temp_to_var = if_else(is.na(!!to_var), !!missing_value, as.character(!!to_var))
) %>%
mutate(
!!new_var := paste(temp_from_var, temp_to_var, sep = !!sep_val)
Expand Down
32 changes: 11 additions & 21 deletions R/derive_vars_dy.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,21 @@ derive_vars_dy <- function(dataset,
abort(err_msg)
}

dy_vars <- if_else(
# named vector passed to `.names` in `across()` to derive name of dy_vars
dy_vars <- set_names(if_else(
source_names == "",
str_replace_all(vars2chr(source_vars), "(DT|DTM)$", "DY"),
source_names
)
), vars2chr(source_vars))

warn_if_vars_exist(dataset, dy_vars)

if (n_vars > 1L) {
dataset %>%
mutate(
across(
.cols = vars2chr(unname(source_vars)),
.fns = list(temp = ~
compute_duration(start_date = !!reference_date, end_date = .))
)
) %>%
rename_with(
.cols = ends_with("temp"),
.fn = ~dy_vars
)
} else {
dataset %>%
mutate(
!!sym(dy_vars) :=
compute_duration(start_date = !!reference_date, end_date = !!source_vars[[1]])
dataset %>%
mutate(
across(
.cols = vars2chr(unname(source_vars)),
.fns = ~ compute_duration(start_date = !!reference_date, end_date = .x),
.names = "{dy_vars}"
)
}
)
}
Loading

0 comments on commit be901c4

Please sign in to comment.