Control the parallelism via threading while calling external packages from mlr3.

For example, the random forest implementation in package ranger (connected via mlr3learners) supports threading via OpenMP. The number of threads to use can be set via hyperparameter num.threads, and defaults to 1. By calling set_threads(x, 4) with x being a ranger learner, the hyperparameter is changed so that 4 cores are used.

If the object x does not support threading, x is returned as-is. If applied to a list, recurses through all list elements.

Note that threading is incompatible with other parallelization techniques such as forking via the future::plan future::multicore. For this reason all learners connected to mlr3 have threading disabled in their defaults.

set_threads(x, n = availableCores())

# S3 method for default
set_threads(x, n = availableCores())

# S3 method for Learner
set_threads(x, n = availableCores())

# S3 method for list
set_threads(x, n = availableCores())



Object to set threads for, e.g. a Learner. This object is modified in-place.


Number of threads to use. Defaults to parallelly::availableCores().


Same object as input x (changed in-place), with possibly updated parameter values.