diff --git a/R/tar_nlmixr.R b/R/tar_nlmixr.R index 1b443d3..7e7bad2 100644 --- a/R/tar_nlmixr.R +++ b/R/tar_nlmixr.R @@ -84,7 +84,7 @@ tar_nlmixr_raw <- function(name, object, data, est, control, table, object_simpl command = substitute( nlmixr_object_simplify(object = object), - list(object = as.name(object)) + list(object = object) ), packages = "nlmixr2est" ), @@ -134,13 +134,16 @@ set_env_object_noinitial <- function(object, env) { if (is.name(object)) { object_env <- env[[as.character(object)]] if (is.function(object_env)) { - object_result <- try(object_env(), silent = TRUE) + object_result <- try(rxode2::assertRxUi(object_env), silent = TRUE) if (inherits(object_result, "rxUi")) { assign(x = as.character(object), value = object_result, envir = env) } } - } else { - stop("Object must be a name") + } else if (is.call(object)) { + # Recursively iterate over all parts of the call + lapply(X = object, FUN = set_env_object_noinitial, env = env) } + # If it's anything other than a name or a call, then we don't need to modify + # it or its sub-objects. NULL } diff --git a/tests/testthat/test-tar_nlmixr.R b/tests/testthat/test-tar_nlmixr.R index 6ed5850..9b66f48 100644 --- a/tests/testthat/test-tar_nlmixr.R +++ b/tests/testthat/test-tar_nlmixr.R @@ -147,6 +147,37 @@ targets::tar_test("tar_nlmixr handling with initial conditions central(0), witho expect_s3_class(pheno, "rxUi") }) +targets::tar_test("tar_nlmixr handling with initial conditions central(0) including model piping, without running the target", { + pheno <- function() { + ini({ + lcl <- log(0.008); label("Typical value of clearance") + lvc <- log(0.6); label("Typical value of volume of distribution") + etalcl + etalvc ~ c(1, + 0.01, 1) + cpaddSd <- 0.1; label("residual variability") + }) + model({ + cl <- exp(lcl + etalcl) + vc <- exp(lvc + etalvc) + kel <- cl/vc + d/dt(central) <- -kel*central + cp <- central/vc + central(0) <- 0 + cp ~ add(cpaddSd) + }) + } + + nlmixr2targets::tar_nlmixr( + name=pheno_model, + object=pheno |> ini(lcl = log(0.1)), + data=nlmixr2data::pheno_sd, + est="saem", + # Minimize time spent + control=nlmixr2est::saemControl(nBurn=1, nEm=1) + ) + + expect_s3_class(pheno, "rxUi") +}) # targets::tar_test() runs the test code inside a temporary directory # to avoid accidentally writing to the user's file space.