nimble-dev / nimble

The base NIMBLE package for R
http://R-nimble.org
BSD 3-Clause "New" or "Revised" License
156 stars 23 forks source link

Expand macro functionality #1361

Closed kenkellner closed 8 months ago

kenkellner commented 10 months ago

This PR expands macro functionality in nimble:

  1. Macros can modify and add constants and add inits
  2. Macros automatically determine which new parameters they created and store this info
  3. "Comments" now allowed in model code (lines containing only a character string)
  4. Macros can auto-generate and add comments to the model code
  5. Extra curly brackets in macro output are cleaned up automatically

Detailed changes

1. Key macro functionality

2. Exported functions

3. Changes to reference classes

4. New nimbleOptions

5. Other stuff:

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 and getMacroParameters, and test-macros.R for example code.

Manual

Nothing yet, not sure if it's the right time to add things to the manual.

kenkellner commented 8 months ago

@perrydv @paciorek I've made nearly all the suggested changes. Just a couple of notes.

perrydv commented 8 months ago

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.