User-defined distributions and nimbleFunctions inside a R package

10 views
Skip to first unread message

­Seowoo Jung

unread,
Aug 27, 2026, 10:36:12 AM (8 days ago) Aug 27
to nimble-users

Hi all,

I'm developing an R package that fits Bayesian single-index models using nimble as the backend. The package registers custom distributions, custom samplers, and helper nimbleFunctions, all defined via nimbleFunction() and used inside nimbleModel() / compileNimble() / nimbleMCMC().

In my current implementation, these objects are assigned to .GlobalEnv at the start of each fitting call and removed on exit, because this is the only setup under which compilation succeeds. Defining them inside a local function environment, a dedicated package environment, or the package namespace causes compilation to fail.

I'd like to move away from this for two reasons: (i) writing to .GlobalEnv from a package call can silently overwrite user objects with the same name, and (ii) exposing internal nimbleFunctions in the user's workspace is confusing for package users.

My questions:

  1. Is it strictly required for user-defined distributions and nimbleFunctions to live in .GlobalEnv for nimbleModel(), compileNimble(), and nimbleMCMC() to work, or is this just the common convention?
  2. If not strictly required, is there a supported way to keep these objects in a dedicated package environment or namespace, and have nimble locate them from there during model building, compilation, and MCMC?

Any pointers to packages that embed nimble this way, or to relevant sections of the manual, would be very helpful.

Thanks in advance,
Seowoo Jung



Chris Paciorek

unread,
Aug 31, 2026, 10:11:55 AM (4 days ago) Aug 31
to ­Seowoo Jung, nimble-users
hi Seowoo,

If you look at the `nimbleEcology` package, you'll see that various
user-defined distributions are created in the package namespace. So I
think following that package as an example might address your
question.

If you take a look and still have questions/problems, please follow up
here and we'll try our best to help.

-chris
> --
> You received this message because you are subscribed to the Google Groups "nimble-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/nimble-users/91cadf0a-6710-4d59-82ea-e84a8f3d3c0bn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages