Skip to content

Commit

Permalink
Merge pull request #137 from mrc-ide/mrc-5605
Browse files Browse the repository at this point in the history
Control compatibility layer with package option
  • Loading branch information
weshinsley authored Jan 21, 2025
2 parents 875cfb8 + 12f8a9b commit 3d0f2fc
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 18 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: odin2
Title: Next generation odin
Version: 0.3.18
Version: 0.3.19
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
person("Wes", "Hinsley", role = "aut"),
Expand Down
7 changes: 4 additions & 3 deletions R/odin.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
##' silently rewrite code, but this is not recommended for general
##' use as eventually the compatibility mode will be removed (this
##' option is primarily intended for comparing output of odin1 and
##' odin2 models against old code).
##' odin2 models against old code). The default, `NULL`, currently
##' corresponds to `warning`.
##'
##' @inheritParams dust2::dust_compile
##'
Expand All @@ -37,7 +38,7 @@
##' y <- dust2::dust_system_simulate(sys, 0:100)
##' matplot(t(y[1, , ]), type = "l", lty = 1, xlab = "Time", ylab = "Value")
odin <- function(expr, input_type = NULL, quiet = NULL, workdir = NULL,
debug = NULL, skip_cache = FALSE, compatibility = "warning") {
debug = NULL, skip_cache = FALSE, compatibility = NULL) {
call <- environment()
dat <- odin_parse_quo(rlang::enquo(expr), input_type, compatibility, call)
code <- generate_dust_system(dat)
Expand Down Expand Up @@ -77,7 +78,7 @@ odin <- function(expr, input_type = NULL, quiet = NULL, workdir = NULL,
##' update(x) <- a
##' a <- Normal(x, 1)
##' }, what = "update")
odin_show <- function(expr, input_type = NULL, compatibility = "warning",
odin_show <- function(expr, input_type = NULL, compatibility = NULL,
what = NULL) {
call <- environment()
dat <- odin_parse_quo(rlang::enquo(expr), input_type, compatibility, call)
Expand Down
5 changes: 3 additions & 2 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
##'
##' # Resulting files:
##' fs::dir_tree(path)
odin_package <- function(path, quiet = FALSE, compatibility = "warning") {
odin_package <- function(path, quiet = FALSE, compatibility = NULL) {
## This finds the package root, and errors (fairly) nicely if it is
## not found.
path <- pkgload::pkg_path(path)
Expand Down Expand Up @@ -100,7 +100,8 @@ odin_package <- function(path, quiet = FALSE, compatibility = "warning") {
}

## Process everything before writing anything:
dat <- lapply(filenames, odin_parse, input_type = "file")
dat <- lapply(filenames, odin_parse,
compatibility = compatibility, input_type = "file")
## For now, take the base name of the input .R file as the class
## name for the generated C++ code; we'll make this configurable
## once we support a general approach for config/options.
Expand Down
13 changes: 11 additions & 2 deletions R/parse.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
odin_parse <- function(expr, input_type = NULL, compatibility = "warning") {
odin_parse <- function(expr, input_type = NULL, compatibility = NULL) {
call <- environment()
odin_parse_quo(rlang::enquo(expr), input_type, compatibility, call)
}


odin_parse_quo <- function(quo, input_type, compatibility, call) {
match_value(compatibility, c("silent", "warning", "error"), call = call)
compatibility <- odin_compatibility_value(compatibility, call)
dat <- parse_prepare(quo, input_type, call)
dat$exprs <- parse_compat(dat$exprs, compatibility, ignore_error = FALSE,
call = call)
Expand Down Expand Up @@ -300,3 +300,12 @@ parse_check_consistent_dimensions_compare <- function(eq, dat, call) {
lapply(eq$rhs$args, parse_check_consistent_dimensions_expr,
eq$src, dat, call)
}


odin_compatibility_value <- function(compatibility, call) {
if (is.null(compatibility)) {
compatibility <- getOption("odin2.compatibility", "warning")
}
valid <- c("warning", "silent", "error")
match_value(compatibility, valid, call = call)
}
3 changes: 1 addition & 2 deletions R/validate.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@
##' deriv(x) <- a
##' a <- user()
##' })
odin_validate <- function(expr, input_type = NULL,
compatibility = "warning") {
odin_validate <- function(expr, input_type = NULL, compatibility = NULL) {
call <- environment()
env <- new.env(parent = emptyenv())
quo <- rlang::enquo(expr)
Expand Down
5 changes: 3 additions & 2 deletions man/odin.Rd

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

5 changes: 3 additions & 2 deletions man/odin_package.Rd

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

5 changes: 3 additions & 2 deletions man/odin_show.Rd

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

5 changes: 3 additions & 2 deletions man/odin_validate.Rd

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

14 changes: 14 additions & 0 deletions tests/testthat/test-parse-compat.R
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,17 @@ test_that("warn about old-style output assignments in arrays", {
"Use `TRUE` on rhs for 'output(x) <- x' expressions",
fixed = TRUE)
})


test_that("select appropriate compatibility mode", {
withr::with_options(list(odin2.compatibility = NULL), {
expect_equal(odin_compatibility_value(NULL, NULL), "warning")
expect_equal(odin_compatibility_value("warning", NULL), "warning")
expect_equal(odin_compatibility_value("error", NULL), "error")
})
withr::with_options(list(odin2.compatibility = "error"), {
expect_equal(odin_compatibility_value(NULL, NULL), "error")
expect_equal(odin_compatibility_value("warning", NULL), "warning")
expect_equal(odin_compatibility_value("error", NULL), "error")
})
})
14 changes: 14 additions & 0 deletions vignettes/details.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,17 @@ Consider discrete time model that compares to data. There will be some series o
1. Read from variables
1. Look up interpolation (using `t0 + dt`)
1. Compare to data

# Options

You can control the *default* behaviour of some odin functions by setting options. All options are prefixed with `odin2.` (this will change to `odin.` once we start actively replacing the old version). To set an option for a session, you can use

```r
options(odin2.whatever = TRUE)
```

If you want to make this permanent, you can add this line to your `~/.Rprofile` file. The easiest way to edit that file is to run `usethis::edit_r_profile()` which will find the correct file and open it in your editor (e.g., RStudio, if you are using that).

## `odin2.compatibility`

The default behaviour of the odin1 to odin2 compatibility checks. If set, this should be a string suitable to pass to `odin2::odin` (one of `warning`, `error` or `silent`); see the help for `odin2::odin` for a description of the possible options.

0 comments on commit 3d0f2fc

Please sign in to comment.