Closed kenkellner closed 10 months ago
@perrydv @paciorek I've made nearly all the suggested changes. Just a couple of notes.
getMacroParameters
- if it becomes as bottleneck for some reason I will revisit this.stop_after_processing_the_model_code
option, you may want to take a closer look at 943a26dac. The problem was that the option wasn't checked until after processBUGScode
was executed (the source of the repeated-index error) and thus had no effect. The comments imply the check should actually happen just after if/else and macro processing, so I moved it up to before processBUGScode
. It makes the option name slightly more confusing, though.Thanks @kenkellner . This looks great and thanks for attending to the points we emailed about. I think the only remaining test failure will be fixed by the --preclean PR and is not due to this PR. It looks like the "stop_after_processing_model_code" option is only used in one place (and did not get much use, I think), so I am fine with slightly repurposing it by the move up a few lines of code that you figured out. That seemed like the only slightly tricky issue and is a simple fix.
My only extremely minor last tweak would be to replace if(nimbleOptions("stop_after_processing_model_code"))
with if(isTRUE(getNimbleOption("stop_after_processing_model_code")))
where the isTRUE
protects against an error if the option is somehow removed from the list or set to some non-logical value and the getNimbleOption
should access a single option more directly.
This PR expands macro functionality in nimble:
Detailed changes
1. Key macro functionality
Expands the section in the
setupModel
method ofmodelDefClass
which is activated only whenenableModelMacros
option isTRUE
. This is where all the macro processing happens, primarily via thecodeProcessModelMacros
function.codeProcessModelMacros
and supporting functions (the vast majority of code in this PR) are now found in the `BUGS_macros.R' file.A new section in the
nimbleModel
function which is activated only whenenableModelMacros
isTRUE
. This section is required to make sure that any modifications to the constants and inits by macros are actually used by nimble.2. Exported functions
getMacroParameters
(see below), marked as EXPERIMENTAL in the title. Has docs via Roxygen.model_macro_builder
(which is already in nimble before this PR) could live in thenimbleMacros
package instead, I'm not sure we ever decided on that.3. Changes to reference classes
modelDefClass
:macroParameters
(a list of parameters created by macros) andmacroInits
(inits created by macros). These are set toNULL
if macros are not enabled.modelDefClass
:assignMacroInits
andassignMacroParameters
for setting the above.modelBaseClass
:getMacroParameters
,getMacroInits
for users to easily access info in the new fields above, andgetConstants
which is just a shortcut to return the constants.4. New nimbleOptions
nimbleOptions
:enableMacroComments
which toggles auto-generation of comments indicating which macro new code came from, andcodeInMacroComments
, which instead of just indicating the macro name in the comments, shows the entire source line of code.5. Other stuff:
enableModelMacros
isTRUE
, nimble will now convert factors (or character vectors/arrays) found in the constants into equivalent numeric form, allowing use of factors in macros like the linear predictor macro I demonstrated. This happens in thecodeProcessModelMacros
function. WhenenableModelMacros
isFALSE
, factors should continue to behave as previously.processBUGSCode
method formodelDefClass
, I made a small change so that nimble ignores (passes through) lines of code which are just a single character string, which I now use as "comments". At the moment this occurs regardless of whether macros are enabled, but doesn't seem to have any side effects.Tests
I've added a bunch of tests for the above in the
test-macros.R
file. They all run cleanly for me. I've struggled to get the full test suite to run locally but ran a few seemingly relevant single test files and all seemed well. Hopefully I haven't overlooked anything.Examples
See the help file for
model_macro_builder
andgetMacroParameters
, andtest-macros.R
for example code.Manual
Nothing yet, not sure if it's the right time to add things to the manual.