Skip to content

Commit 4ce4d5e

Browse files
lona-kbe-marc
andauthored
feat: add cli package for class printer (#129)
* Use cli package for class printer * Change mlr3misc import to Remotes --------- Co-authored-by: be-marc <marcbecker@posteo.de>
1 parent c7bf883 commit 4ce4d5e

9 files changed

+44
-27
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Depends:
2929
Imports:
3030
bbotk (>= 1.5.0.9000),
3131
checkmate (>= 2.0.0),
32+
cli,
3233
data.table,
3334
lgr,
3435
mlr3misc (>= 0.15.1),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export(trm)
5353
export(trms)
5454
import(bbotk)
5555
import(checkmate)
56+
import(cli)
5657
import(data.table)
5758
import(mlr3)
5859
import(mlr3misc)

R/ArchiveBatchFSelect.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ ArchiveBatchFSelect = R6Class("ArchiveBatchFSelect",
158158
#'
159159
#' @param ... (ignored).
160160
print = function() {
161-
catf(format(self))
161+
cat_cli(cli_h1("{.cls {class(self)[1]}}"))
162162
print(self$data[, setdiff(names(self$data), "uhash"), with = FALSE], digits=2)
163163
},
164164

R/AutoFSelector.R

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,25 @@ AutoFSelector = R6Class("AutoFSelector",
234234
#' Printer.
235235
#' @param ... (ignored).
236236
print = function() {
237-
catf(format(self))
238-
catf(str_indent("* Model:", if (is.null(self$model)) "-" else class(self$model)[1L]))
239-
catf(str_indent("* Packages:", self$packages))
240-
catf(str_indent("* Predict Type:", self$predict_type))
241-
catf(str_indent("* Feature Types:", self$feature_types))
242-
catf(str_indent("* Properties:", self$properties))
237+
msg_h = if (is.null(self$label) || is.na(self$label)) "" else paste0(": ", self$label)
238+
model = if (is.null(self$model)) "-" else class(self$model)[1L]
239+
240+
cat_cli({
241+
cli_h1("{.cls {class(self)[1L]}} ({self$id}){msg_h}")
242+
cli_li("Model: {model}")
243+
cli_li("Packages: {.pkg {self$packages}}")
244+
cli_li("Predict Type: {self$predict_type}")
245+
cli_li("Feature Types: {self$feature_types}")
246+
cli_li("Properties: {self$properties}")
247+
})
248+
243249
w = self$warnings
244250
e = self$errors
245251
if (length(w)) {
246-
catf(str_indent("* Warnings:", w))
252+
cat_cli(cli_alert_warning("Warnings: {w}"))
247253
}
248254
if (length(e)) {
249-
catf(str_indent("* Errors:", e))
255+
cat_cli(cli_alert_danger("Errors: {e}"))
250256
}
251257
}
252258
),

R/EnsembleFSResult.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ EnsembleFSResult = R6Class("EnsembleFSResult",
139139
#'
140140
#' @param ... (ignored).
141141
print = function(...) {
142-
catf("%s with %s learners and %s initial resamplings",
143-
format(self), self$n_learners, self$n_resamples)
142+
cat_cli(cli_h1("{.cls {class(self)[1L]}} with {.val {self$n_learners}} learners and {.val {self$n_resamples}} initial resamplings"))
144143
print(private$.result[, c("resampling_iteration", "learner_id", "n_features"), with = FALSE])
145144
},
146145

R/FSelectInstanceBatchMultiCrit.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,16 @@ FSelectInstanceBatchMultiCrit = R6Class("FSelectInstanceBatchMultiCrit",
113113
#'
114114
#' @param ... (ignored).
115115
print = function(...) {
116-
catf(format(self))
117-
catf(str_indent("* State: ", if (is.null(private$.result)) "Not optimized" else "Optimized"))
118-
catf(str_indent("* Objective:", format(self$objective)))
119-
catf(str_indent("* Terminator:", format(self$terminator)))
116+
cli_h1("{.cls {class(self)[1]}}")
117+
is_optimized = if (is.null(private$.result)) "Not optimized" else "Optimized"
118+
cli_li("State: {is_optimized}")
119+
cli_li("Objective: {.cls {class(self$objective)[1]}} ({self$objective$id})")
120+
cli_li("Terminator: {.cls {class(self$terminator)[1]}}")
121+
120122
if (!is.null(private$.result)) {
121-
catf("* Result:")
123+
cli_li("Result:")
122124
print(self$result[, c(self$archive$cols_x, self$archive$cols_y), with = FALSE])
123-
catf("* Archive:")
125+
cli_li("Archive")
124126
print(as.data.table(self$archive)[, c(self$archive$cols_x, self$archive$cols_y), with = FALSE])
125127
}
126128
}

R/FSelectInstanceBatchSingleCrit.R

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,18 @@ FSelectInstanceBatchSingleCrit = R6Class("FSelectInstanceBatchSingleCrit",
156156
#'
157157
#' @param ... (ignored).
158158
print = function(...) {
159-
catf(format(self))
160-
catf(str_indent("* State: ", if (is.null(private$.result)) "Not optimized" else "Optimized"))
161-
catf(str_indent("* Objective:", format(self$objective)))
162-
catf(str_indent("* Terminator:", format(self$terminator)))
159+
is_optimized = if (is.null(private$.result)) "Not optimized" else "Optimized"
160+
cat_cli({
161+
cli_h1("{.cls {class(self)[1L]}}")
162+
cli_li("State: {is_optimized}")
163+
cli_li("Objective: {.cls {class(self$objective)[1]}} ({self$objective$id})")
164+
cli_li("Terminator: {.cls {class(self$terminator)[1]}}")
165+
})
166+
163167
if (!is.null(private$.result)) {
164-
catf("* Result:")
168+
cat_cli(cli_li("Result:"))
165169
print(self$result[, c(self$archive$cols_x, self$archive$cols_y), with = FALSE])
166-
catf("* Archive:")
170+
cat_cli(cli_li("Archive:"))
167171
print(as.data.table(self$archive)[, c(self$archive$cols_x, self$archive$cols_y), with = FALSE])
168172
}
169173
}

R/FSelector.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ FSelector = R6Class("FSelector",
6868
#'
6969
#' @return (`character()`).
7070
print = function() {
71-
catn(format(self), if (is.na(self$label)) "" else paste0(": ", self$label))
72-
catf(str_indent("* Parameters:", as_short_string(self$param_set$values)))
73-
catf(str_indent("* Properties:", self$properties))
74-
catf(str_indent("* Packages:", self$packages))
71+
msg_h = if (is.na(self$label)) "" else paste0(": ", self$label)
72+
cat_cli({
73+
cli_h1("{.cls {class(self)[1L]}}{msg_h}")
74+
cli_li("Parameters: {as_short_string(self$param_set$values)}")
75+
cli_li("Properties: {self$properties}")
76+
cli_li("Packages: {.pkg {self$packages}}")
77+
})
7578
},
7679

7780
#' @description

R/zzz.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#' @import data.table
22
#' @import checkmate
3+
#' @import cli
34
#' @import paradox
45
#' @import mlr3misc
56
#' @import mlr3

0 commit comments

Comments
 (0)