Skip to content

Commit

Permalink
Merge pull request #800 from nlmixr2/798-simplify-repeated-ifelse
Browse files Browse the repository at this point in the history
simplify repeated ifelse
  • Loading branch information
mattfidler authored Nov 21, 2024
2 parents 6a3e8ab + 10da9b4 commit 42b4a6e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
66 changes: 32 additions & 34 deletions R/et.R
Original file line number Diff line number Diff line change
Expand Up @@ -1324,48 +1324,46 @@ c.rxEvid <- function(x, ...) {
}
.colorFmt.rxEvid <- function(x, ...) {
.x <- unclass(x)
.x <-
ifelse(.x == 0, paste0(crayon::blue$bold("0"), ":", crayon::white("Observation")),
ifelse(.x == 1, paste0(crayon::blue$bold("1"), ":", crayon::yellow("Dose (Add)")),
ifelse(.x == 2, paste0(crayon::blue$bold("2"), ":", crayon::yellow("Other")),
ifelse(.x == 3, paste0(crayon::blue$bold("3"), ":", crayon::red("Reset")),
ifelse(.x == 4, paste0(crayon::blue$bold("4"), ":", crayon::red("Reset"), "&", crayon::yellow("Dose")),
ifelse(.x == 5, paste0(crayon::blue$bold("5"), ":", crayon::red("Replace")),
ifelse(.x == 6, paste0(crayon::blue$bold("6"), ":", crayon::yellow("Multiply")),
ifelse(.x == 7, paste0(crayon::blue$bold("7"), ":", crayon::yellow("Transit")),
paste0(crayon::blue$red(.x), ":", crayon::red("Invalid")))
)
)
)
)
)
if (is.numeric(.x)) {
.x <-
data.table::fcase(
.x == 0, paste0(crayon::blue$bold("0"), ":", crayon::white("Observation")),
.x == 1, paste0(crayon::blue$bold("1"), ":", crayon::yellow("Dose (Add)")),
.x == 2, paste0(crayon::blue$bold("2"), ":", crayon::yellow("Other")),
.x == 3, paste0(crayon::blue$bold("3"), ":", crayon::red("Reset")),
.x == 4, paste0(crayon::blue$bold("4"), ":", crayon::red("Reset"), "&", crayon::yellow("Dose")),
.x == 5, paste0(crayon::blue$bold("5"), ":", crayon::red("Replace")),
.x == 6, paste0(crayon::blue$bold("6"), ":", crayon::yellow("Multiply")),
.x == 7, paste0(crayon::blue$bold("7"), ":", crayon::yellow("Transit")),
default=paste0(crayon::blue$red(.x), ":", crayon::red("Invalid"))
)
)
return(format(.x, justify = "left"))
} else {
.x <- paste0(crayon::blue$red(.x), ":", crayon::red("Invalid"))
}
format(.x, justify = "left")
}

#' @rdname rxEvid
#' @export
as.character.rxEvid <- function(x, ...) {
.x <- unclass(x)
.x <-
ifelse(.x == 0, "0:Observation",
ifelse(.x == 1, "1:Dose (Add)",
ifelse(.x == 2, "2:Other",
ifelse(.x == 3, "3:Reset",
ifelse(.x == 4, "4:Reset&Dose",
ifelse(.x == 5, "5:Replace",
ifelse(.x == 6, "6:Multiply",
ifelse(.x == 7, "7:Transit",
paste0(.x, ":Invalid"))
)
)
)
)
)
if (is.numeric(.x)) {
.x <-
data.table::fcase(
.x == 0, "0:Observation",
.x == 1, "1:Dose (Add)",
.x == 2, "2:Other",
.x == 3, "3:Reset",
.x == 4, "4:Reset&Dose",
.x == 5, "5:Replace",
.x == 6, "6:Multiply",
.x == 7, "7:Transit",
default = paste0(.x, ":Invalid")
)
)
return(.x)
} else {
.x <- paste0(.x, ":Invalid")
}
.x
}


Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-et.R
Original file line number Diff line number Diff line change
Expand Up @@ -829,3 +829,20 @@ rxTest({

})
})

test_that("as.character.rxEvid", {
expect_equal(
as.character.rxEvid(-1:9),
c("-1:Invalid", "0:Observation", "1:Dose (Add)", "2:Other", "3:Reset",
"4:Reset&Dose", "5:Replace", "6:Multiply", "7:Transit", "8:Invalid",
"9:Invalid")
)
expect_equal(
as.character.rxEvid("A"),
"A:Invalid"
)
expect_equal(
as.character.rxEvid(0.5),
"0.5:Invalid"
)
})

0 comments on commit 42b4a6e

Please sign in to comment.