Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion R/add_default_output_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ add_default_output_tables <- function(
## goodness of fit, all rows
if("fit" %in% tables && !(default_table_names[["fit"]] %in% existing_tables)) {
if(verbose) cli::cli_alert_info("Adding output table for goodness of fit")
cols <- c("DV", "EVID", "MDV", "PRED", "IPRED", "CWRES", "NPDE")
optional_vars <- c("MDV", "IPRED")
add_optional <- c()
for(variab in optional_vars) {
check_var <- check_nm_table_variables(model, variab, throw_error = FALSE)
if(is.null(check_var)) { # i.e. IPRED is declared as variable and we can safely add to table
add_optional <- c(add_optional, variab)
}
}
cols <- c("DV", "EVID", add_optional, "PRED", "CWRES", "NPDE")
if(full_tables) {
cols <- unique(c(cols, model$datainfo$names))
}
Expand Down
29 changes: 18 additions & 11 deletions R/add_table_to_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ add_table_to_model <- function(
#' Check NONMEM table variables
#'
#' @inheritParams add_table_to_model
#' @param throw_error should throw error? If FALSE, will return vector of
#' invalid variables.
#'
#' @returns `NULL` if all variables are valid, otherwise an error.
check_nm_table_variables <- function(model, variables) {
#' @returns `NULL` if all variables are valid, otherwise an error (or return
#' all invalid variables if `throw_error` is FALSE).
#'
check_nm_table_variables <- function(model, variables, throw_error = TRUE) {
is_data_variable <- variables %in% model$datainfo$names
is_lhs_variable <- variables %in% get_lhs_variables(model)
is_defined_pk_parameter <- variables %in% get_defined_pk_parameters(model)
Expand All @@ -67,15 +71,18 @@ check_nm_table_variables <- function(model, variables) {
if (all(is_valid)) return()

invalid_variables <- variables[!is_valid]
cli::cli_abort(c(
paste0(
"$TABLE variables must be any of the following: ",
"A data variable, a left-hand side variable in the model, ",
"or a reserved NONMEM $TABLE variable."
),
"x" = "{.field {invalid_variables}} {?is/are} not {?a/} valid variable{?s}.",
"i" = "Did you make a typo?"
))
if(throw_error) {
cli::cli_abort(c(
paste0(
"$TABLE variables must be any of the following: ",
"A data variable, a left-hand side variable in the model, ",
"or a reserved NONMEM $TABLE variable."
),
"x" = "{.field {invalid_variables}} {?is/are} not {?a/} valid variable{?s}.",
"i" = "Did you make a typo?"
))
}
invalid_variables
}

get_lhs_variables <- function(model) {
Expand Down
30 changes: 15 additions & 15 deletions R/create_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,21 @@ create_model <- function(
parameter_uncertainty_method = toupper(uncertainty_method)
)
}


## Convert to TDMDD model?
if(!is.null(tmdd_type)) {
allowed_tmdd_types <- c("full", "ib", "crib", "cr", "qss", "mmapp")
if(!tmdd_type %in% allowed_tmdd_types) {
cli::cli_abort("Specified `tmdd_type` not allowed, select one of: {allowed_tmdd_types}.")
}
cli::cli_alert_info("Converting model into TMDD structure ({tmdd_type}).")
mod <- mod |>
pharmr::set_tmdd(type = tmdd_type)
}

## Add metabolite compartment?
mod <- add_metabolite_compartment(mod, metabolite, route)

## Add $TABLEs
if(tool == "nonmem") {
if(!is.null(tables)) {
Expand Down Expand Up @@ -323,20 +337,6 @@ create_model <- function(
)
}

## Convert to TDMDD model?
if(!is.null(tmdd_type)) {
allowed_tmdd_types <- c("full", "ib", "crib", "cr", "qss", "mmapp")
if(!tmdd_type %in% allowed_tmdd_types) {
cli::cli_abort("Specified `tmdd_type` not allowed, select one of: {allowed_tmdd_types}.")
}
cli::cli_alert_info("Converting model into TMDD structure ({tmdd_type}).")
mod <- mod |>
pharmr::set_tmdd(type = tmdd_type)
}

## Add metabolite compartment?
mod <- add_metabolite_compartment(mod, metabolite, route)

## Handle BLQ
if(!is.null(blq_method)) {
blq_method <- tolower(blq_method)
Expand Down
8 changes: 6 additions & 2 deletions man/check_nm_table_variables.Rd

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

Loading