Skip to content

Commit

Permalink
720 Remove replace_emptys_with_na (#768)
Browse files Browse the repository at this point in the history
Closes #720

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
edelarua and github-actions[bot] authored Dec 9, 2022
1 parent bf2b2e8 commit 09d1a6b
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 107 deletions.
2 changes: 0 additions & 2 deletions R/df_explicit_na.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
#' # Encode missing values in a subset of columns.
#' df_explicit_na(my_data, omit_columns = c("x", "y"))
#'
#' @seealso [replace_emptys_with_na()]
df_explicit_na <- function(data,
omit_columns = NULL,
char_as_factor = TRUE,
Expand Down Expand Up @@ -86,7 +85,6 @@ df_explicit_na <- function(data,
}

if (is.factor(xi) || is.character(xi)) {

# Handle empty strings and NA values.
xi <- explicit_na(sas_na(xi), label = na_level)

Expand Down
6 changes: 2 additions & 4 deletions R/logistic_regression.R
Original file line number Diff line number Diff line change
Expand Up @@ -904,11 +904,10 @@ logistic_summary_by_flag <- function(flag_var) {
#' @param drop_and_remove_str string to be dropped and removed
#'
#' @examples
#' # Internal function - replace_emptys_with_na
#' \dontrun{
#' # flagging empty strings with "_"
#' df <- replace_emptys_with_na(df, rep_str = "_")
#' df2 <- replace_emptys_with_na(df2, rep_str = "_")
#' df <- df_explicit_na(df, na_level = "_")
#' df2 <- df_explicit_na(df2, na_level = "_")
#'
#' result1 <- basic_table() %>%
#' summarize_logistic(
Expand All @@ -931,7 +930,6 @@ logistic_summary_by_flag <- function(flag_var) {
summarize_logistic <- function(lyt,
conf_level,
drop_and_remove_str = "") {

# checks
checkmate::assert_string(drop_and_remove_str)

Expand Down
39 changes: 0 additions & 39 deletions R/utils_factor.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,42 +285,3 @@ fct_collapse_only <- function(.f, ..., .na_level = "<Missing>") {
x <- forcats::fct_collapse(.f, ..., other_level = .na_level)
do.call(forcats::fct_relevel, args = c(list(.f = x), as.list(new_lvls)))
}

#' Replace all empty string values in data frame
#'
#' @description
#' Function used to fix for update in `Rtables issue` `tern#593` (`NA` alternative -> `" "`)
#' This function is similar in scope as `df_explicit_na()`. In the future a merge
#' is to be expected.
#'
#' @param df `data.frame` table to act upon
#' @param rep_str replacement string for empty strings
#'
#' @details this functions relies onto `for` loop, `levels`, and `nchar`. It can
#' easily be optimized for different cases. For the moment, this fits the purpose
#' to fix issue `tern#593`
#'
#' @seealso [df_explicit_na()]
#'
#' @keywords internal
replace_emptys_with_na <- function(df, rep_str = "NA") {

# checks
checkmate::assert_string(rep_str)

# col logical v
where_to_mod <- apply(df, 2, function(x) any(nchar(x) == 0))

# main loop on cols
for (cl_nm in which(where_to_mod)) {
tmp_cl <- df[, cl_nm]
if (is.null(levels(tmp_cl))) {
tmp_cl[sapply(tmp_cl, nchar) == 0] <- rep_str
} else {
levels(tmp_cl)[sapply(levels(tmp_cl), nchar) == 0] <- rep_str
}
df[, cl_nm] <- tmp_cl
}

return(df)
}
2 changes: 0 additions & 2 deletions man/df_explicit_na.Rd

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

5 changes: 2 additions & 3 deletions man/logistic_regression.Rd

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

27 changes: 0 additions & 27 deletions man/replace_emptys_with_na.Rd

This file was deleted.

21 changes: 7 additions & 14 deletions tests/testthat/test-logistic_regression.R
Original file line number Diff line number Diff line change
Expand Up @@ -1022,17 +1022,15 @@ testthat::test_that("summarize_logistic works as expected for interaction model
interaction = "AGE"
)
)
df <- broom::tidy(mod1, conf_level = 0.99)

# fix for update in Rtables tern#593
df2 <- replace_emptys_with_na(df, rep_str = "_") # _ is the flag value
df <- broom::tidy(mod1, conf_level = 0.99) %>%
df_explicit_na(na_level = "_")

result <- basic_table() %>%
summarize_logistic(
conf_level = 0.99,
drop_and_remove_str = "_"
) %>%
build_table(df2)
build_table(df)

result_matrix <- to_string_matrix(result)
expected_matrix <- structure(
Expand Down Expand Up @@ -1072,11 +1070,8 @@ testthat::test_that("summarize_logistic works as expected for interaction model
interaction = "SEX"
)
)

df <- broom::tidy(model, conf_level = 0.99)

# fix for update in Rtables tern#593
df <- replace_emptys_with_na(df, rep_str = "_")
df <- broom::tidy(model, conf_level = 0.99) %>%
df_explicit_na(na_level = "_")

result <- basic_table() %>%
summarize_logistic(
Expand Down Expand Up @@ -1119,10 +1114,8 @@ testthat::test_that("summarize_logistic works as expected for simple model witho
adrs,
variables = list(response = "Response", arm = "ARMCD", covariates = "AGE")
)
df <- broom::tidy(mod1, conf_level = 0.99)

# fix for update in Rtables tern#593
df <- replace_emptys_with_na(df, rep_str = "_")
df <- broom::tidy(mod1, conf_level = 0.99) %>%
df_explicit_na(na_level = "_")

result <- basic_table() %>%
summarize_logistic(
Expand Down
24 changes: 8 additions & 16 deletions tests/testthat/test-table_lgrt02.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ testthat::test_that("LGRT02 without interaction term is produced correctly", {
variables = list(response = "Response", arm = "ARMCD", covariates = c("SEX", "RACE", "AGE"))
)
conf_level <- 0.95
df <- broom::tidy(model, conf_level = conf_level)

# fix for update in Rtables tern#593
df <- replace_emptys_with_na(df, rep_str = "_")
df <- broom::tidy(model, conf_level = conf_level) %>%
df_explicit_na(na_level = "_")

result <- basic_table() %>%
summarize_logistic(
Expand Down Expand Up @@ -89,10 +87,8 @@ testthat::test_that("LGRT02 with categorical interaction is produced correctly",
)
)
conf_level <- 0.95
df <- broom::tidy(model, conf_level = conf_level)

# fix for update in Rtables tern#593
df <- replace_emptys_with_na(df, rep_str = "_")
df <- broom::tidy(model, conf_level = conf_level) %>%
df_explicit_na(na_level = "_")

result <- basic_table() %>%
summarize_logistic(
Expand Down Expand Up @@ -142,10 +138,8 @@ testthat::test_that("LGRT02 with continuous interaction is produced correctly",
)
)
conf_level <- 0.95
df <- broom::tidy(model, conf_level = conf_level, at = c(18, 65))

# fix for update in Rtables tern#593
df <- replace_emptys_with_na(df, rep_str = "_")
df <- broom::tidy(model, conf_level = conf_level, at = c(18, 65)) %>%
df_explicit_na(na_level = "_")

result <- basic_table() %>%
summarize_logistic(
Expand Down Expand Up @@ -195,10 +189,8 @@ testthat::test_that("LGRT02 with setting values indicating an event and custom a
response_definition = "1 - response"
)
conf_level <- 0.9
df <- broom::tidy(model, conf_level = conf_level)

# fix for update in Rtables tern#593
df <- replace_emptys_with_na(df, rep_str = "_")
df <- broom::tidy(model, conf_level = conf_level) %>%
df_explicit_na(na_level = "_")

result <- basic_table() %>%
summarize_logistic(
Expand Down

0 comments on commit 09d1a6b

Please sign in to comment.