Skip to contents

Update generated code in a package that uses odin and dust to provide a model. This will generate new dust code in inst/dust and from that generate a full model in src, and an R interface in R/dust.R, along with the cpp11 attributes that are needed to use the model.

Usage

odin_package(path, quiet = FALSE, compatibility = "warning")

Arguments

path

Path to the package root (the directory that contains DESCRIPTION), or any path within that package.

quiet

Logical, indicating if compilation messages from pkgbuild should be displayed. Error messages will be displayed on compilation failure regardless of the value used. If NULL is given, then we take the value from DUST_QUIET if set, or FALSE otherwise.

compatibility

Compatibility mode to use. Valid options are "warning", which updates code that can be fixed, with warnings, and "error", which will error. The option "silent" will 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).

Value

Invisibly, the path to the package. However, this function is typically called for its side effect of updating files in inst/dust and src within this package after you have changed the odin code in inst/odin.

Details

This function is powered by dust2::dust_package, and the same pre-requisites apply here:

For your DESCRIPTION file:

  • dust2 must be in Imports

  • cpp11, dust2 and monty must be in LinkingTo

For your NAMESPACE file:

  • you must have a suitable useDynLib() call with .registration = TRUE

If you do not satisfy these requirements, dust2::dust_package will fail with a message indicating actions you should take. Once set up, generally things will keep working.

If you want your packages to build on GitHub actions, or be installable via remotes::install_github you should add to your DESCRIPTION:

Remotes: mrc-ide/dust2, mrc-ide/monty

Note that you do not need to include odin2 itself as a dependency.

See also

vignette("packaging") for more details, and dust2::dust_package(), which does most of the work here.

Examples

# An example package structure
fs::dir_tree(path)
#> /tmp/RtmpKXbqVo/file166768f3c2a9
#> ├── DESCRIPTION
#> ├── NAMESPACE
#> └── inst
#>     └── odin
#>         └── sir.R

# Generate odin code:
odin_package(path)
#>  Found 1 odin code file in 'inst/odin'
#>  Wrote 'inst/dust/sir.cpp'
#>  Working in package 'example' at '/tmp/RtmpKXbqVo/file166768f3c2a9'
#>  Found 1 system
#>  Wrote 'src/sir.cpp'
#>  Wrote 'R/dust.R'
#>  Wrote 'src/Makevars'
#>  12 functions decorated with [[cpp11::register]]
#>  generated file cpp11.R
#>  generated file cpp11.cpp

# Resulting files:
fs::dir_tree(path)
#> /tmp/RtmpKXbqVo/file166768f3c2a9
#> ├── DESCRIPTION
#> ├── NAMESPACE
#> ├── R
#> │   ├── cpp11.R
#> │   └── dust.R
#> ├── inst
#> │   ├── dust
#> │   │   └── sir.cpp
#> │   └── odin
#> │       └── sir.R
#> └── src
#>     ├── Makevars
#>     ├── cpp11.cpp
#>     └── sir.cpp