Skip to content

Commit

Permalink
Replace deprecated forcats::fct_explicit_na (#817)
Browse files Browse the repository at this point in the history
Closes #811
  • Loading branch information
edelarua authored Feb 9, 2023
1 parent 4786665 commit 681e57a
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 35 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Imports:
cowplot,
dplyr,
emmeans (>= 1.4.5),
forcats,
forcats (>= 1.0.0),
formatters (>= 0.3.4.13),
ggplot2,
grid,
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
* Updated tests to use `testthat` 3rd edition.
* Created separate documentation files for functions in different sections of pkgdown reference.
* Created separate `.R` files for logistic regression and cox regression helper functions.
* Removed all template tests from `tern`. These have been relocated to internal repo `scda.test`.
* Removed all template tests from `tern`. These tests are in internal repo `scda.test`.
* Replaced deprecated function `forcats::fct_explicit_na` with `forcats::fct_na_value_to_level`.

# tern 0.7.10

Expand Down
4 changes: 2 additions & 2 deletions R/missing_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ explicit_na <- function(x, label = "<Missing>") {
checkmate::assert_string(label)

if (is.factor(x)) {
forcats::fct_explicit_na(x, label)
x <- forcats::fct_na_value_to_level(x, label)
forcats::fct_drop(x, only = label)
} else if (is.character(x)) {
x[is.na(x)] <- label
x
Expand All @@ -30,7 +31,6 @@ explicit_na <- function(x, label = "<Missing>") {
}
}


#' Convert Strings to `NA`
#'
#' @description `r lifecycle::badge("stable")`
Expand Down
5 changes: 3 additions & 2 deletions R/utils_factor.R
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ fct_discard <- function(x, discard) {
#' @param na_level (`string`)\cr which level to use for missings.
#'
#' @return The modified factor with inserted and existing `NA` converted to `na_level`.
#' @seealso [forcats::fct_explicit_na()] which is used internally.
#' @seealso [forcats::fct_na_value_to_level()] which is used internally.
#'
#' @examples
#' fct_explicit_na_if(factor(c("a", "b", NA)), c(TRUE, FALSE, FALSE))
Expand All @@ -253,7 +253,8 @@ fct_explicit_na_if <- function(x, condition, na_level = "<Missing>") {
checkmate::assert_factor(x, len = length(condition))
checkmate::assert_logical(condition)
x[condition] <- NA
forcats::fct_explicit_na(x, na_level = na_level)
x <- forcats::fct_na_value_to_level(x, level = na_level)
forcats::fct_drop(x, only = na_level)
}

#' Collapsing of Factor Levels and Keeping Only Those New Group Levels
Expand Down
2 changes: 1 addition & 1 deletion man/fct_explicit_na_if.Rd

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

30 changes: 30 additions & 0 deletions tests/testthat/_snaps/df_explicit_na.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,36 @@
3 A A A F <Missing> <Missing> 3 NA
4 B <Missing> <Missing> E C C 4 NA

---

Code
res
Output
$v1
[1] "B" "A"
$v2
[1] "B" "A" "<Missing>"
$v3
[1] "B" "A" "<Missing>"
$v4
[1] "D" "E" "F"
$v5
[1] "A" "B" "C" "<Missing>"
$v6
[1] "A" "B" "C" "<Missing>"
$v7
NULL
$v8
NULL

# Default settings work when input data does not have labels

Code
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/_snaps/summarize_num_patients.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
——————————————————————————————————————————————————————————————————
Number of patients with at least one event 3 (60.0%) 3 (75.0%)
Number of events 4 4
(n) 3 3
(n) 3 3

---

Expand Down Expand Up @@ -122,7 +122,7 @@
A B
(N=5) (N=4)
————————————————————
(n) 3 3
(n) 3 3

# s_num_patients count_by works as expected with healthy input

Expand Down Expand Up @@ -260,7 +260,7 @@
——————————————————————————————————————————————————————————————————
Number of patients with at least one event 3 (60.0%) 3 (75.0%)
Number of events 3 3
(n) 3 3
(n) 3 3

---

Expand Down Expand Up @@ -290,7 +290,7 @@
A B
(N=5) (N=4)
————————————————————
(n) 3 3
(n) 3 3

# summarize_num_patients with count_by different combinations works as expected with healthy input

Expand All @@ -302,7 +302,7 @@
——————————————————————————————————————————————————————————————————
Number of patients with at least one event 3 (60.0%) 3 (75.0%)
Number of events 4 3
(n) 3 3
(n) 3 3

# analyze_num_patients works well for pagination

Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-df_explicit_na.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ testthat::test_that("Default fill in of missing values and conversion to factor

res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)

res <- testthat::expect_silent(sapply(result, levels))
testthat::expect_snapshot(res)
})

testthat::test_that("Default settings work when input data does not have labels", {
Expand Down
23 changes: 0 additions & 23 deletions tests/testthat/test-g_lineplot.R

This file was deleted.

0 comments on commit 681e57a

Please sign in to comment.