Skip to content

Random tips and tricks

Daniel Turek edited this page Mar 10, 2025 · 27 revisions
  • To check that a C++ compiler installed:
devtools::has_devel()
  • To see the internal adaptation info from adaptive samplers:
nimbleOptions(buildInterfacesForCompiledNestedNimbleFunctions=TRUE)
Cmcmc$samplerFunctions$contentsList[[i]]$propCov
  • To build & install developmental version from github:
remove.packages('nimble')
library(remotes)
remotes::install_github('nimble-dev/nimble', ref = 'devel', subdir = 'packages/nimble')
library(nimble)
  • Get samplesPlot() and chainsPlot() MCMC plotting functions:
library(basicMCMCplots)
  • When package build fails around release, possibly with error library not found for -lgfortran, in src/Makevars.in, comment out FLIBS to read: #$(FLIBS)

  • To clear all loaded DLLs w.r.t. any project (pass any object from that project):

nimble:::clearCompiled(Rmodel)
  • Access / assign to a variable in a nested compiled nimbleFunction (without using the option nimbleOptions(buildInterfacesForCompiledNestedNimbleFunctions = TRUE)):
valueInCompiledNimbleFunction(outerNF$nestedNF, 'x')
valueInCompiledNimbleFunction(outerNF$nestedNF, 'x', newValue)
valueInCompiledNimbleFunction(Cmcmc$samplerFunctions[[i]], 'x')
valueInCompiledNimbleFunction(Cmcmc$samplerFunctions[[i]], 'x', newValue)
  • Dig one level deeper into compiled objects, e.g., access an internal state variable, from within a nested compiled nimbleFunction:
valueInCompiledNimbleFunction(Rmcmc$samplerFunctions$contentsList[[1]]$my_calcAdaptationFactor$.CobjectInterface, 'gamma1')
valueInCompiledNimbleFunction(Cmcmc$Robject$samplerFunctions$contentsList[[1]]$my_calcAdaptationFactor$.CobjectInterface, 'gamma1')
  • Get ancestors (parents) of a model node:
md <- model$getModelDef()
parentIDs <- md$maps$edgesFrom[ md$maps$edgesTo == md$nodeName2GraphIDs(node) ]
parentNodes <- md$maps$graphID_2_nodeName[ parentIDs ]
  • Fixing Windows compilation error: In system(cmd) : 'make' not found
currentPath <- Sys.getenv('PATH')
newPath <- paste0('C:\\Rtools\\bin;C:\\Rtools\\mingw_64\\bin;', currentPath)
Sys.setenv(PATH = newPath)
vignette('Introduction_to_nimbleEcology', package = 'nimbleEcology')
  • Debugging generated C++ code:
nimbleOptions(debugCppLineByLine = TRUE)
  • Issues using install_github, in particular:
install_github("nimble-dev/nimble", ref = "devel", subdir = "packages/nimble")
## The R package directory has not been created yet
## ** libs
## Error: (converted from warning) this package has a non-empty 'configure.win' file, so building only the main architecture
- Get `samplesPlot()` and `chainsPlot()` MCMC plotting functions:

In this case, try (1) in R execute the line Sys.setenv(R_REMOTES_NO_ERRORS_FROM_WARNINGS = "true"), and then run the install_github, and (2) add INSTALL_opts="--no-multiarch" as an argument to install_github.

  • Rcpp does not compile on Mac OS Monterey, R 4.1.2., clang error that looks like:
Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir,  : 
  Error 1 occurred building shared library.
clang: error: no such file or directory: '/Library/Frameworks/R.framework/Resources/lib/libc++abi.1.dylib'
make: *** [sourceCpp_2.so] Error 1

In R type:

unlink("~/.R/Makevars")
unlink("~/.Renviron")

Then restart R, and in R try Rcpp::evalCpp("1+1")

  • If failing to compile objects on Mac OSC, from a terminal type xcode-select -p. If the output is something like /Library/Developer/CommandLineTools, that's ok. If it's like /Library/Developer, then you need to run the command sudo xcode-select -s /Library/Developer/CommandLineTools.

  • To debug compiled execution (crashes), use the little-used trick:

nimbleOptions(debugCppLineByLine = TRUE)

which will emit a message before every line of compiled C++ to see where a crash occurs.

  • To "debug" the steps of configureMCMC:
nimble:::MCMCconf$trace('initialize', browser)

Clone this wiki locally