Skip to content

Commit

Permalink
Closes #2014 derive_var_shift change na_val@devel (#2032)
Browse files Browse the repository at this point in the history
* #2014 `na_val` deprecated and `missing_value` inserted into `derive_var_shift`.

* #2014 - Update `NEWS.md`.

* #2014 - update tests and write deprecation parameter test

* #2014 - run required tasks for PR

---------

Co-authored-by: Ben Straub <ben.x.straub@gsk.com>
  • Loading branch information
sophie-gem and bms63 authored Jul 28, 2023
1 parent c4213d3 commit 2e487b4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 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")
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
- 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)

## Documentation


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
9 changes: 6 additions & 3 deletions man/derive_var_shift.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 36 additions & 5 deletions tests/testthat/test-derive_var_shift.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
test_that("Shift based on character variables", {
# derive_var_shift ----

## Test 1: Shift based on character variables ----
test_that("derive_var_shift Test 1: Shift based on character variables", {
input <- tibble::tribble(
~USUBJID, ~PARAMCD, ~AVAL, ~ABLFL, ~BNRIND, ~ANRIND,
"P01", "ALB", 33, "Y", "LOW", "LOW",
Expand Down Expand Up @@ -28,7 +31,8 @@ test_that("Shift based on character variables", {
})


test_that("Shift based on character variables with missing values", {
## Test 2: Shift based on character variables with missing values ----
test_that("derive_var_shift Test 2: Shift based on character variables with missing values", {
input <- tibble::tribble(
~USUBJID, ~PARAMCD, ~AVAL, ~ABLFL, ~BNRIND, ~ANRIND,
"P01", "ALB", 33, "Y", "LOW", "LOW",
Expand Down Expand Up @@ -60,7 +64,8 @@ test_that("Shift based on character variables with missing values", {
})


test_that("Shift based on numeric variables with missing values", {
## Test 3: Shift based on numeric variables with missing values ----
test_that("derive_var_shift Test 3: Shift based on numeric variables with missing values", {
input <- tibble::tribble(
~USUBJID, ~PARAMCD, ~AVAL, ~ABLFL, ~BASE,
"P01", "ALB", 33.1, "Y", 33.1,
Expand Down Expand Up @@ -91,7 +96,8 @@ test_that("Shift based on numeric variables with missing values", {
)
})

test_that("Shift with user-specified na_val and sep_val", {
## Test 4: Shift with user-specified missing_value and sep_val ----
test_that("derive_var_shift Test 4: Shift with user-specified missing_value and sep_val", {
input <- tibble::tribble(
~USUBJID, ~PARAMCD, ~AVAL, ~ABLFL, ~BNRIND, ~ANRIND,
"P01", "ALB", 33, "Y", "LOW", "LOW",
Expand All @@ -117,9 +123,34 @@ test_that("Shift with user-specified na_val and sep_val", {
new_var = SHIFT1,
from_var = BNRIND,
to_var = ANRIND,
na_val = "MISSING",
missing_value = "MISSING",
sep_val = " - "
),
expected_output
)
})

## Test 5: Test deprecation warning of na_val argument ----
test_that("derive_var_shift Test 5: Test deprecation warning of na_val argument", {
input <- tibble::tribble(
~USUBJID, ~PARAMCD, ~AVAL, ~ABLFL, ~BNRIND, ~ANRIND,
"P01", "ALB", 33, "Y", "LOW", "LOW",
"P01", "ALB", 38, NA, "LOW", "NORMAL",
"P01", "ALB", NA, NA, "LOW", NA,
"P02", "ALB", NA, "Y", NA, NA,
"P02", "ALB", 49, NA, NA, "HIGH",
"P02", "SODIUM", 147, "Y", "HIGH", "HIGH"
)

expect_warning(
derive_var_shift(
input,
new_var = SHIFT1,
from_var = BNRIND,
to_var = ANRIND,
na_val = "MISSING",
sep_val = " - "
),
class = "lifecycle_warning_deprecated"
)
})

0 comments on commit 2e487b4

Please sign in to comment.