Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/update dots #1068

Merged
merged 8 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: mrgsolve
Title: Simulate from ODE-Based Models
Version: 1.0.9
Version: 1.0.9.9000
Authors@R:
c(person(given = "Kyle T", family = "Baron",
role = c("aut", "cre"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ importFrom(rlang,is_named)
importFrom(rlang,quo_name)
importFrom(rlang,quos)
importFrom(rlang,syms)
importFrom(rlang,warn)
importFrom(stats,as.formula)
importFrom(stats,rnorm)
importFrom(stats,setNames)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mrgsolve (development version)

# mrgsolve 1.0.9

- Fix signatures for `compiled.mrgmod()` and `as_tibble.mrgsims()` based on new
Expand Down
2 changes: 1 addition & 1 deletion R/Aaaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#' @importFrom magrittr %>%
#' @importFrom tibble tibble as_tibble
#' @importFrom rlang quos enquo enquos quo_name syms !!! !! eval_tidy as_label
#' @importFrom rlang is_named .data abort
#' @importFrom rlang is_named .data abort warn
#' @importFrom lifecycle deprecate_soft deprecate_warn
#' @importFrom glue glue
#' @importFrom Rcpp evalCpp
Expand Down
4 changes: 2 additions & 2 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#' - `mrgolve.project`: sets the default project director ([mread()])
#' - `mrgsolve.soloc`: sets the default package build directory ([mread()])
#' - `mrgsolve_mread_quiet`: don't print messages during [mread()]
#' - `mrgsolve.update.strict`: if `TRUE`, print warning when trying to update
#' an item in the model object that doesn't exist
#' - `mrgsolve.update.strict`: this option has been deprecated; use the `strict`
#' argument to [mrgsolve::update()] instead
#'
#'
#' @description
Expand Down
54 changes: 35 additions & 19 deletions R/update.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ all_updatable <- c(sval,other_val)
#'
#' After the model object is created, update various attributes.
#'
#' @param object a model object
#' @param ... named items to update
#' @param object a model object.
#' @param ... named items to update.
#' @param merge logical indicating to merge (rather than replace)
#' new and existing attributes
#' new and existing attributes.
#' @param open logical; used only when merge is \code{TRUE} and
#' parameter list or initial conditions
#' list is being updated; if \code{FALSE}, no new items will be
#' added; if \code{TRUE}, the parameter list may expand.
#' @param data a list of items to update; this list is combined
#' with any items passed in via \code{...}
#' @param strict if \code{TRUE}, then an error will be generated if there is
#' attempt to update a non-existent item
#' with any items passed in via \code{...}.
#' @param strict if \code{TRUE}, a warning will be issued when there is an
#' attempt to update a non-existent item.
#'
#' @return The updated model object is returned.
#'
Expand Down Expand Up @@ -79,19 +79,22 @@ all_updatable <- c(sval,other_val)
#'
#' @examples
#' \dontrun{
#' mod <- mrgsolve::house()
#' mod <- house()
##'
#' mod <- update(mod, end=120, delta=4, param=list(CL=19.1))
#' mod <- update(mod, end = 120, delta = 4, param = list(CL = 19.1))
#' }
#'
#' @seealso \code{\link{update}}, \code{\link{mrgmod-class}},
#' \code{\link{within}}
#'
#' @export
#'
setMethod("update", "mrgmod", function(object, ..., merge=TRUE, open=FALSE,
data=NULL, strict=TRUE) {

# TODO: get rid of the strict argument to `update()`
# TODO: get rid of the merge argument to `update()`
# TODO: get rid of the open argument to `update()`
# TODO: longer term ... error when bad arguments are passed

args <- list(...)

if(!is.null(data)) {
Expand All @@ -104,17 +107,30 @@ setMethod("update", "mrgmod", function(object, ..., merge=TRUE, open=FALSE,

a <- names(args)

m <- charmatch(a,all_updatable)
m <- charmatch(a, all_updatable)

if(strict && anyNA(m)) {
if(getOption("mrgsolve.update.strict", FALSE)) {
bad <- a[is.na(m)]
for(b in bad) {
mesg <- paste0("invalid item for model object update: ", b)
warning(mesg, call.=FALSE, immediate.=TRUE)
}
if(anyNA(m) && isTRUE(strict)) {
if(!is.null(getOption("mrgsolve.update.strict"))) {
msg <- c("The `mrgsolve.update.strict` option has been deprecated;",
"please use the `strict` argument to `mrgsolve::update()`",
"instead.")
warn(paste0(msg, collapse = " "))
}
bad <- a[is.na(m)]
names(bad) <- rep("x", length(bad))
if(length(bad) > 1) {
msg <- c("The following arguments were passed to `mrgsolve::update()`,",
"they are either invalid names (check your spelling?) or not",
"eligible attributes for update:")
} else {
msg <- c("The following argument was passed to `mrgsolve::update()`, but",
"it is either an invalid name (check your spelling?) or not an",
"eligible attribute for update:")
}
msg <- c(msg, bad)
warn(msg, call = NULL)
}

valid <- !is.na(m)
a[valid] <- all_updatable[m[valid]]
names(args) <- a
Expand Down Expand Up @@ -326,7 +342,7 @@ setMethod("update", "parameter_list", function(object, .y, ...) {
wstop("[param-update] parameters must be single numeric values.")
}
}

object@data <- update_list(
object@data,
.y,
Expand Down
4 changes: 2 additions & 2 deletions man/mrgsolve_package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions man/update.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions tests/testthat/test-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ test_that("model caches via mread_cache", {

saveRDS(mo,cache_file)

mod2 <- mread_cache("pk1", modlib(),compile=FALSE)
mod2 <- mread_cache("pk1", modlib(), compile = FALSE)

expect_equal(mod2@args$foo,mo@args$foo)
expect_equal(mod2@args$foo, mo@args$foo)

unlink(cache_file)
})

test_that("model caches via mcode_cache", {
Expand Down
20 changes: 17 additions & 3 deletions tests/testthat/test-update.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,23 @@ test_that("Solver setting rtol updates", {
})

test_that("bad update gives warning", {
options(mrgsolve.update.strict=TRUE)
expect_warning(update(mod, kyle = 1), "invalid item for model object update")
options(mrgsolve.update.strict=FALSE)
expect_warning(
update(mod, kyle = 1),
"The following argument was passed"
)
expect_warning(
update(mod, kyle = 1, baron = 2),
"The following arguments were passed"
)
})

test_that("the mrgsolve.update.strict option is deprecated", {
options(mrgsolve.update.strict = TRUE)
expect_warning(
update(house(), foo = 2),
regexp="mrgsolve\\.update\\.strict",
)
options(mrgsolve.update.strict = NULL)
})

test_that("update outvars issue-483", {
Expand Down