diff --git a/R/tar_nlmixr_multimodel.R b/R/tar_nlmixr_multimodel.R index d5443b0..c6014de 100644 --- a/R/tar_nlmixr_multimodel.R +++ b/R/tar_nlmixr_multimodel.R @@ -4,10 +4,12 @@ #' modelFunction` #' @inheritParams nlmixr2est::nlmixr #' @inheritParams targets::tar_target +#' @param envir The environment where models are defined (usually doesn't need +#' to be modified) #' @return A list of targets for the model simplification, data simplification, #' and model estimation. #' @export -tar_nlmixr_multimodel <- function(name, ..., data, est, control = list(), table = nlmixr2est::tableControl()) { +tar_nlmixr_multimodel <- function(name, ..., data, est, control = list(), table = nlmixr2est::tableControl(), envir = parent.frame()) { if (is.null(est)) { stop("'est' must not be null") } @@ -20,11 +22,12 @@ tar_nlmixr_multimodel <- function(name, ..., data, est, control = list(), table table = substitute(table), # This extracts the ... argument similarly to using `substitute()`. From # https://stackoverflow.com/questions/55019441/deparse-substitute-with-three-dots-arguments - model_list = match.call(expand.dots = FALSE)$... + model_list = match.call(expand.dots = FALSE)$..., + envir = envir ) } -tar_nlmixr_multimodel_parse <- function(name, data, est, control, table, model_list) { +tar_nlmixr_multimodel_parse <- function(name, data, est, control, table, model_list, envir) { checkmate::assert_named(model_list, type = "unique") ret_prep <- lapply( @@ -34,7 +37,8 @@ tar_nlmixr_multimodel_parse <- function(name, data, est, control, table, model_l data = data, est = est, control = control, - table = table + table = table, + envir = envir ) # Extract the targets to fit. This will be a list of lists. The inner list # will have the three targets for fitting the model, and the outer list will @@ -42,7 +46,7 @@ tar_nlmixr_multimodel_parse <- function(name, data, est, control, table, model_l target_model_fitting <- lapply(X = unname(ret_prep), FUN = \(x) x[["target"]]) # Generate the combined list with names combined_list <- lapply(X = ret_prep, FUN = \(x) as.name(x$name)) - target_combined_list <- tar_target_raw(name = name, command = str2lang(deparse(combined_list))) + target_combined_list <- targets::tar_target_raw(name = name, command = str2lang(deparse(combined_list))) # Return the models to fit and the list-combining target append( target_model_fitting, @@ -50,10 +54,10 @@ tar_nlmixr_multimodel_parse <- function(name, data, est, control, table, model_l ) } -tar_nlmixr_multimodel_single <- function(object, name, data, est, control, table) { +tar_nlmixr_multimodel_single <- function(object, name, data, est, control, table, envir) { # Hash the model itself without its description. Then, if the description # changes, the model will not need to rerun. - hash_long <- digest::digest(eval(object)) + hash_long <- digest::digest(eval(object, envir = envir)) hash <- substr(hash_long, 1, 8) name_hash <- paste(name, hash, sep = "_") tar_prep <- diff --git a/man/tar_nlmixr_multimodel.Rd b/man/tar_nlmixr_multimodel.Rd index 3ea303a..67ebc03 100644 --- a/man/tar_nlmixr_multimodel.Rd +++ b/man/tar_nlmixr_multimodel.Rd @@ -10,7 +10,8 @@ tar_nlmixr_multimodel( data, est, control = list(), - table = nlmixr2est::tableControl() + table = nlmixr2est::tableControl(), + envir = parent.frame() ) } \arguments{ @@ -42,6 +43,9 @@ to be different for each type of estimation method} \item{table}{The output table control object (like `tableControl()`)} + +\item{envir}{The environment where models are defined (usually doesn't need +to be modified)} } \value{ A list of targets for the model simplification, data simplification,