Skip to content

Conversation

adamlilith
Copy link

glueNimbleCode() combines output from different calls to nimbleCode() so it can be processed by nimbleModel().

Useful for cases where we have the same basic nimble code, but want to tack on different blocks depending on the workflow. Obviates having a different script for every small difference between models. A very simple example:

code_start <- nimbleCode({
   sigma ~ dunif(0, 100)
   for (i in 1:n) {
      y[i] ~ dnorm(mu, sd = sigma)
   }
})

broad_prior <- nimbleCode({ mu ~ dnorm(0, sd = 100) })
reg_prior_prior <- nimbleCode({ mu ~ ddexp(rate = 1) })

if (dataset == 'a') {
   model_code <- glueNimbleCode(code_start, broad_prior)
} else if (dataset == 'b') {
   model_code <- glueNimbleCode(code_start, reg_prior)
}

model <- nimbleModel(model_code, ...) # etc

`glueNimbleCode()` combines output from different calls to `nimbleCode()`
@paciorek
Copy link
Contributor

@adamlilith thanks for this contribution. This is a nice idea and we can appreciate the appeal of being able to glue together different model components.

We've had a bit of discussion of this on the dev team about whether to accomplish this as you've outlined or instead allow nimbleCode to take ... , allowing a user to provide multiple model code chunks that way.

I'm hopeful we'll have something along these lines in the next release (which isn't too far away), though it might not be exactly your approach.

@adamlilith
Copy link
Author

adamlilith commented Jul 19, 2025 via email

@paciorek
Copy link
Contributor

We're taking the approach of adding functionality to nimbleCode in the nimbleCode_combine_code_objects branch.

@paciorek paciorek closed this Aug 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants