mlr3
mlr3 copied to clipboard
Test that learners can be initialized without installing the required packages
It should be possible to initialize a learner without having the required package installed. Usually this also works, however for the
learner "clust.ap" from mlr3cluster this is not the case. Because the learner uses the package apcluster to construct the default arguments,
lrn("clust.ap") only works when the package apcluster is already installed. This also means that the install_learners function does not work for "clust.ap" as it depends on constructing the learner first and then accessing the $packages slot.
Maybe the simplest way to solve this it to mention it in the template for new learners
Answering as install_learners is a mlr3extralearners function (unless someone has coded a different version, @mllg ?). The function does not depend on the required packages to construct a learner, your example is also not reproducible for me
library(mlr3cluster)
library(mlr3)
lrn("clust.ap")
#> <LearnerClustAP:clust.ap>
#> * Model: -
#> * Parameters: s=<function>
#> * Packages: apcluster
#> * Predict Type: partition
#> * Feature types: logical, integer, numeric
#> * Properties: complete, exclusive, partitional
Created on 2021-11-16 by the reprex package (v2.0.1)
Did you remove.packages("apcluster") before?
library(mlr3cluster)
#> Loading required package: mlr3
lrn("clust.ap")
#> Error in loadNamespace(x): there is no package called 'apcluster'
Created on 2021-11-16 by the reprex package (v2.0.1)
Oops you are so right. This definitely didn't used to happen otherwise it would have bugged in extralearners before! Will let someone else answer properly. Ignore me !
extra_pkgs = function(key, pkgs = character()) {
pkgs = union("mlr3", pkgs)
for (pkg in pkgs) {
requireNamespace(pkg, quietly = TRUE)
}
snap = loadedNamespaces()
mlr3::lrn(key)
setdiff(snap, loadedNamespaces())
}
callr::r(extra_pkgs, args = list("classif.rpart"))
callr::r(extra_pkgs, args = list("classif.ranger", "mlr3learners"))
Would this work?