Skip to content

Commit c5908a9

Browse files
committed
wrap reg type logic into function; use throughout code
1 parent 67ab5ff commit c5908a9

5 files changed

Lines changed: 61 additions & 13 deletions

File tree

R/advan_create_data.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ advan_create_data <- function(
1818
covariate_model = NULL) {
1919

2020
## Set up basic structure
21-
is_bolus <- regimen$type == "bolus" | grepl("oral", regimen$type)
21+
is_bolus <- is_bolus_or_oral(regimen)
2222
data <- data.frame(
2323
ID = 1,
2424
TIME = regimen$dose_times,

R/create_event_table.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ create_event_table <- function(
122122
# parse list to a design (data.frame)
123123
# For boluses, oral, and covariates, set infusion time to 0. For all
124124
# other methods, assume infusion time was provided correctly
125-
is_bolus <- regimen$type %in% c("bolus", "covariate") | grepl("oral", regimen$type)
125+
is_bolus <- regimen$type %in% c("covariate") | is_bolus_or_oral(regimen)
126126
regimen$t_inf[is_bolus] <- 0
127127
dos <- data.frame(cbind(t = regimen$dose_times,
128128
dose = regimen$dose_amts,

R/new_regimen.R

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,20 @@ new_regimen <- function(
127127
if(length(reg$type) != length(reg$dose_times)) {
128128
reg$type <- rep(reg$type[1], length(reg$dose_times))
129129
}
130-
if(any(reg$type %in% c("infusion", "sc", "im"))) {
131-
if(any(reg$t_inf == 0)) {
132-
reg$t_inf[reg$t_inf == 0] <- 1/60
133-
reg$rate[reg$t_inf == 0] <- 60
130+
# set oral/bolus t_inf to zero, and non-oral/bolus t_inf to minimum 1 minute
131+
idx_bolus_oral <- is_bolus_or_oral(reg)
132+
if (!all(idx_bolus_oral)) {
133+
idx_infusion_0length <- reg$t_inf == 0 & !idx_bolus_oral
134+
if (any(idx_infusion_0length)) {
135+
reg$t_inf[idx_infusion_0length] <- 1/60
136+
reg$rate[idx_infusion_0length] <- 60
134137
}
135138
}
136-
if(any(reg$type == "bolus")) {
137-
reg$t_inf[reg$type == "bolus"] <- 0
138-
reg$rate[reg$type == "bolus"] <- 0
139-
}
140-
if(any(grepl("oral", reg$type))) {
141-
reg$t_inf[grepl("oral", reg$type)] <- 0
142-
reg$rate[grepl("oral", reg$type)] <- 0
139+
if(any(is_bolus_or_oral(reg))) {
140+
reg$t_inf[is_bolus_or_oral(reg)] <- 0
141+
reg$rate[is_bolus_or_oral(reg)] <- 0
143142
}
143+
144144
if(!is.null(cmt)) {
145145
if(length(cmt) != length(reg$dose_times)) {
146146
cmt <- rep(cmt[1], length(reg$dose_times))
@@ -160,3 +160,17 @@ new_regimen <- function(
160160
}
161161
return(reg)
162162
}
163+
164+
#' Is regimen type oral or bolus? (i.e., treat t_inf as zero)
165+
#'
166+
#' Any regimen matching exactly the string 'bolus' or containing the string
167+
#' 'oral' will return `TRUE`, otherwise `FALSE`. Bolus/oral regimens are
168+
#' generally treated as having an infusion duration of zero.
169+
#'
170+
#' @param regimen object of class "regimen"`
171+
#' @returns Returns a logical vector of length equal to the number of doses in
172+
#' the regimen
173+
174+
is_bolus_or_oral <- function(regimen) {
175+
regimen$type == "bolus" | grepl("oral", regimen$type)
176+
}

man/is_bolus_or_oral.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_new_regimen.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,17 @@ test_that("t_inf imputed correctly", {
121121
expect_equal(reg5$t_inf, c(2/60, 2.5, 3/60, 1))
122122
})
123123

124+
test_that("regimens of type bolus/oral are recognized as such", {
125+
expect_equal(
126+
is_bolus_or_oral(
127+
new_regimen(interval = 24, type = c("oral_x", "infusion", "sc"))
128+
),
129+
c(TRUE, FALSE, FALSE)
130+
)
131+
expect_equal(
132+
is_bolus_or_oral(
133+
new_regimen(interval = 24, type = c("bolus_unknown", "infusion", "oral"))
134+
),
135+
c(FALSE, FALSE, TRUE)
136+
)
137+
})

0 commit comments

Comments
 (0)