mlr3 icon indicating copy to clipboard operation
mlr3 copied to clipboard

Test that learners can be initialized without installing the required packages

Open sebffischer opened this issue 4 years ago • 5 comments

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

sebffischer avatar Nov 16 '21 12:11 sebffischer

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)

RaphaelS1 avatar Nov 16 '21 13:11 RaphaelS1

Did you remove.packages("apcluster") before?

sebffischer avatar Nov 16 '21 13:11 sebffischer

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)

sebffischer avatar Nov 16 '21 13:11 sebffischer

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 !

RaphaelS1 avatar Nov 16 '21 13:11 RaphaelS1

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?

mllg avatar Nov 23 '21 13:11 mllg