Skip to content

Commit

Permalink
Merge pull request #29 from nlmixr2/handle-known-dsl-differently-for-n1
Browse files Browse the repository at this point in the history
Fix known elements in dsl
  • Loading branch information
mattfidler authored Sep 14, 2024
2 parents 2f0f53a + 5a3b87a commit ce16d48
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
22 changes: 20 additions & 2 deletions R/lotri.R
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,14 @@ NULL
}
}

.isKnownCall <- function(x) {
if (is.call(x) && length(x) >= 1) {
return(tolower(as.character(x[[1]])) %in%
c("fix", "fixed", "unfix", "unfixed", "var", "sd", "cor", "cov", "chol"))
}
FALSE
}

#' Handle Matrix Expressions with Tilde
#'
#' This function processes matrix expressions of the form `name ~ c(lower-tri)`.
Expand Down Expand Up @@ -653,7 +661,8 @@ NULL
.x3 <- .x3t
}
} else if (length(.x3) == 2L &&
.isFixedElt(.x3[[1]])) {
.isFixedElt(.x3[[1]]) &&
!.isKnownCall(.x3[[2]])) {
.x3t <- .x3
.x3t[[1]] <- quote(`c`)
.x3t <- eval(.x3t, envir=.lotriParentEnv)
Expand All @@ -662,14 +671,23 @@ NULL
.fix <- TRUE
}
} else if (length(.x3) == 2L &&
.isUnfixedElt(.x3[[1]])) {
.isUnfixedElt(.x3[[1]]) &&
!.isKnownCall(.x3[[2]])) {
.x3t <- .x3
.x3t[[1]] <- quote(`c`)
.x3t <- eval(.x3t, envir=.lotriParentEnv)
if (length(.x3t) == 1L && is.numeric(.x3t)) {
.x3 <- .x3t
.unfix <- TRUE
}
} else if (length(.x3) == 2L &&
!.isKnownCall(.x3)){
.x3t <- try(eval(.x3, envir=.lotriParentEnv), silent=TRUE)
if (!inherits(.x3t, "try-error") &&
length(.x3t) == 1L &&
is.numeric(.x3t)) {
.x3 <- .x3t
}
}
if (length(.x3) == 1) {
.resetLastN(env)
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-line-form.R
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,18 @@ test_that("lotri lower triangular matrix specification 2", {
lotri(eta1+eta2 ~ c(0.175278, 0.115896, 0.112362),
eta3 ~ 0))

expect_error(lotri(eta.cl+eta.v~fix(cor(sd(0.3,0.02,0.1)))),
NA)

expect_equal(lotri({
eta1 ~ 0.175278
eta2 ~ c(0.115896, 0.112362)
eta3 ~ sqrt(24)
}), lotri({
eta1 + eta2 ~ c(0.175278, 0.115896, 0.112362)
eta3 ~ sqrt(24)
}))

})

})

0 comments on commit ce16d48

Please sign in to comment.