Skip to content

Commit

Permalink
Work with piped objects
Browse files Browse the repository at this point in the history
  • Loading branch information
billdenney committed Mar 9, 2024
1 parent 801fc6c commit 2802fb3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
11 changes: 7 additions & 4 deletions R/tar_nlmixr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
),
Expand Down Expand Up @@ -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
}
31 changes: 31 additions & 0 deletions tests/testthat/test-tar_nlmixr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 2802fb3

Please sign in to comment.