View source: R/export_models.R
| export_models | R Documentation |
Create C++ code to export TMB models from package.
export_models(pkg = ".")
pkg |
Character string: any subdirectory of the package source code. |
TMB models should be saved as C++ header files of the form src/TMB/*.hpp, and written almost exactly as with usual TMB *.cpp models. So for example, src/TMB/ModelA.hpp would be written as:
// __DO NOT__ '#include <TMB.hpp>' as file is not include-guarded
#undef TMB_OBJECTIVE_PTR
#define TMB_OBJECTIVE_PTR obj
// name of function _must_ match file name (ModelA)
template<class Type>
Type ModelA(objective_function<Type>* obj) {
// _exactly_ the same code as for usual 'ModelA.cpp'
}
#undef TMB_OBJECTIVE_PTR
#define TMB_OBJECTIVE_PTR this
The function export_models creates a file src/TMB/pkgname_TMBExports.cpp containing a single TMB model object which dispatches the appropriate ModelA.hpp, ModelB.hpp, etc. using if/else statements. At the R level, the correct model is invoked from TMB::MakeADFun exactly as for a single TMB model, except the data list argument gets an additional element model specifying the name of the model, e.g., model = "ModelA".
export_models assumes that each file of the form src/TMB/*.hpp contains exactly one TMB model. In order for these to #include additional .hpp files, these additional files must be placed either in a subfolder of src/TMB, or in (a subfolder of) inst/include. The advantage of the latter approach is that the additional files are available to other R packages via LinkingTo: pkgname is the other package's DESCRIPTION. If the latter approach is used, the TMB compiler must be notified of the additional include directory. This is done by setting the TMB_FLAGS in src/Makevars[.win] to
TMB_FLAGS = -I"../../inst/include"
Other flags specific to the TMB compiler can be set here as well, as can the usual CXX_FLAGS, etc. for other source code in src, which is compiled independently of that in src/TMB.
Invisible; called for its side effects.
use_tmb
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.