From b3eb09bc485bf7f3301cd4ff852520f3acb445ee Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Wed, 29 Nov 2023 20:04:12 -0600 Subject: [PATCH 1/2] Add failing test --- tests/testthat/test-ui-piping.R | 44 ++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-ui-piping.R b/tests/testthat/test-ui-piping.R index b8c4c5547..7fc54c6e6 100644 --- a/tests/testthat/test-ui-piping.R +++ b/tests/testthat/test-ui-piping.R @@ -1921,12 +1921,12 @@ test_that("piping with append=lhs", { test_that("test ui appending of derived variables like `sim` can work", { - + one.compartment <- function() { ini({ tka <- 0.45 - tcl <- 1 - tv <- 3.45 + tcl <- 1 + tv <- 3.45 eta.ka ~ 0.6 eta.cl ~ 0.3 eta.v ~ 0.1 @@ -1946,5 +1946,41 @@ test_that("test ui appending of derived variables like `sim` can work", { f <- rxode2(one.compartment) expect_error(model(f$simulationModel, sim2=sim+1, append=sim), NA) - + +}) + + +test_that("off-diagonal piping issue #518", { + + mod <- function() { + ini({ + a <- 1 + b <- 2 + etaa + etab ~ c(3, 0.1, 4) + c <- 5 + etac ~ 6 + d <- 7 + f <- 9 + etad + etaf ~ c(8, 0.2, 10) + }) + model({ + g <- (a + etaa)/(b + etab) + h <- (c + etac) + i <- (d + etad) + j <- f + etaf + }) + } + + modNew <- + ini( + rxode2(mod), + etab + etac + etad ~ + c(7, + 0.2, 8, + 0.3, 0.4, 9), + etaa ~ 0 + ) + + expect_error(modNew$omega, NA) + }) From 361132dbdf2f3ed72f4ea310668890c97b894f7e Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Wed, 29 Nov 2023 20:04:19 -0600 Subject: [PATCH 2/2] Add piping ini fix --- R/piping-ini.R | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/R/piping-ini.R b/R/piping-ini.R index 7cc279c62..4aa99fb16 100644 --- a/R/piping-ini.R +++ b/R/piping-ini.R @@ -474,6 +474,24 @@ # (Maybe) update parameter order; this must be at the end so that the # parameter exists in case it is promoted from a covariate .iniHandleAppend(expr = expr, rxui = rxui, envir = envir, append = append) + + # now take out ETAs that no longer exist + .iniDf <- get("iniDf", envir=rxui) + .w <- which(is.na(.iniDf$neta1) & !is.na(.iniDf$neta2)) + .reassign <- FALSE + if (length(.w) > 0) { + .iniDf <- .iniDf[-.w, ] + .reassign <- TRUE + } + .iniDf <- get("iniDf", envir=rxui) + .w <- which(!is.na(.iniDf$neta1) & is.na(.iniDf$neta2)) + if (length(.w) > 0) { + .iniDf <- .iniDf[-.w, ] + .reassign <- TRUE + } + if (.reassign) { + assign("iniDf", .iniDf, envir=rxui) + } } # TODO: while nlmixr2est is changed