[R-pkg-devel] A way to prepend/append some code before/after running package examples? (e.g. to setup CPU resource usage limits for package example run or to clean up generated files)

Durif Ghislain gh|@|@|n@dur|| @end|ng |rom en@-|yon@|r
Fri Jul 11 10:18:26 CEST 2025


Hi again,

Thanks @Dirk for pointing the issue with the encoding of my original 
message (it cannot be read from the list archive either). I do not 
understand what could have been the problem since there was no issue 
with other mails sent to the list earlier on the same day in the same 
condition.  I am resending it below from my webmail instead of my 
mailer, hoping it will work better this time, and so that everyone can 
see my original message.

Regarding my issue, I did not know about the \dontshow tag, so I will 
use it. Thanks.

Best

----

Hi,

We have a package that runs parallel computations (work in progress, can 
be found here 
https://github.com/getkeops/keops/tree/fix_rkeops_v2_keops_v2.3/rkeops).

The package deeply relies on a Python package through `reticulate` that 
itself interfaces OpenMP-based C++ code, which can thus run parallel 
computations.

Users of our package can control CPU resource usage thanks to a wrapper 
function around `RhpcBLASctl::omp_set_num_threads(n_core)` available in 
our package.

By default, OpenMP uses all available cores which is a bad practice for 
CRAN checks. It is recommended to only use 2 CPU cores for computation 
per CRAN policies (which is totally understandable).

For unit test run with `testthat`, we can use a `tests/testthat/setup.R` 
file to run any arbitrary code before running the tests, so I can setup 
a limit on CPU usage here.

For my Rmarkdown vignette build, I have a code chunk not included in the 
output document that setup a limit using our wrapper function.

However, regarding example run, is there a way to automatically setup 
some resource limit before running package examples, or to automatically 
run some code (i.e. add code at top of generated `<pkg>-Ex.R` file) 
before running all the examples?

If not, I will have to repeat the function call that setup limit before 
every examples in my package (around 70 exported functions in my 
package), which does not seems to be really optimal.

In the same spirit, generally my example run breaks because `reticulate` 
is not explicitly initialized (I am not facing this issue in interactive 
or standard script mode and I do not understand why), so I would need to 
run something like `reticulate::py_available(initialize = TRUE)` or 
`reticulate::py_config()` before running my examples (so to prepend this 
at the top of the generated example file).

Also, our package generate cache files in `tools::R_user_dir()` 
directory because it does JIT compilation (and the generated library 
files are cached for reuse). We have a function to clean them, so I 
would need to call this function after example runs so that these files 
are removed (again for CRAN check).

Note: at the moment I have \dontrun tags around almost all my examples 
to avoid all these issues, but it was highlighted in a recent CRAN 
submission that it should be reduced to a minimum if possible.

Thanks in advance,
Best regards,


---
Ghislain Durif
-------------------------------------------
Research engineer -- CNRS
LBMC, ENS Lyon (France)
Webpage: https://gdurif.perso.math.cnrs.fr



More information about the R-package-devel mailing list