Dear Luca and Chris,
Thank you very much for your replies.
Following your suggestions, I tried to avoid assigning the NIMBLE distribution functions to the global environment. The solution that worked in my case was to register the distribution and, when running chains in parallel, explicitly export the two NIMBLE distribution functions to each worker. So my internal helper function now registers the distribution with NIMBLE, without using assign(..., envir = globalenv()):
registerDistributions(list(
dcar_leroux = list(
BUGSdist = "dcar_leroux(rho, sd, Lambda,
from.to, zero_mean)",
types = c(
"value = double(1)",
"rho = double(0)",
"sd = double(0)",
"Lambda = double(1)",
"
from.to = double(2)",
"zero_mean = double(0)"
),
pqAvail = FALSE
)
))
Then, in the parallel version of my function, after creating the cluster and loading nimble in each worker, I added:
parallel::clusterExport(my.cluster,
varlist = c("dcar_leroux", "rcar_leroux"),
envir = environment(dcar_leroux))
This makes dcar_leroux() and rcar_leroux() available inside each worker, so NIMBLE can find them when processing the model code in parallel. This seems to solve the problem without assigning the functions to the user's global environment, and the R CMD check NOTE caused by those global assignments disappears.
Thanks again for pointing me in the right direction.
Best regards,
Miguel Ángel