Skip to content

Commit 562b54f

Browse files
committed
Add default param, use cli classes
1 parent 6628711 commit 562b54f

File tree

3 files changed

+199
-26
lines changed

3 files changed

+199
-26
lines changed

R/control.R

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,43 +73,42 @@ control_grid <- function(
7373
}
7474

7575
# Helper function to print control settings using cli
76-
print_control_settings <- function(x) {
76+
print_control_settings <- function(x, default = FALSE, defaults = NULL) {
7777
# Get the fields to print
7878
fields <- names(x)
7979

80+
# Optionally reduce to only non-defaults
81+
if (default && !is.null(defaults)) {
82+
fields <- fields[
83+
!vapply(
84+
fields,
85+
function(field) {
86+
identical(x[[field]], defaults[[field]])
87+
},
88+
logical(1)
89+
)
90+
]
91+
}
92+
8093
# Build formatted lines for each field
8194
for (field in fields) {
8295
value <- x[[field]]
8396

84-
# Format the value based on type
85-
if (is.null(value)) {
86-
formatted_value <- "NULL"
87-
} else if (is.function(value)) {
88-
formatted_value <- "<function>"
89-
} else if (is.logical(value)) {
90-
formatted_value <- as.character(value)
91-
} else if (is.numeric(value)) {
92-
formatted_value <- format(value)
93-
} else if (is.character(value)) {
94-
if (length(value) == 1) {
95-
formatted_value <- paste0("'", value, "'")
96-
} else {
97-
formatted_value <- paste0("[", paste(value, collapse = ", "), "]")
98-
}
97+
if (is.function(value)) {
98+
cli::cli_bullets(c(" " = "{.arg {field}}: <function>"))
9999
} else if (inherits(value, "tune_backend_options")) {
100-
formatted_value <- "<backend_options>"
100+
cli::cli_bullets(c(" " = "{.arg {field}}: <backend_options>"))
101101
} else {
102-
formatted_value <- paste0("<", class(value)[1], ">")
102+
cli::cli_bullets(c(" " = "{.arg {field}}: {.val {value}}"))
103103
}
104-
105-
cli::cli_bullets(c(" " = paste0("{.field ", field, "}: ", formatted_value)))
106104
}
107105
}
108106

109107
#' @export
110-
print.control_grid <- function(x, ...) {
108+
print.control_grid <- function(x, default = FALSE, ...) {
111109
cli::cli_text("{.emph Grid/resamples control object}")
112-
print_control_settings(x)
110+
defaults <- control_grid()
111+
print_control_settings(x, default = default, defaults = defaults)
113112
invisible(x)
114113
}
115114

@@ -150,9 +149,10 @@ control_last_fit <- function(
150149
}
151150

152151
#' @export
153-
print.control_last_fit <- function(x, ...) {
152+
print.control_last_fit <- function(x, default = FALSE, ...) {
154153
cli::cli_text("{.emph Last fit control object}")
155-
print_control_settings(x)
154+
defaults <- control_last_fit()
155+
print_control_settings(x, default = default, defaults = defaults)
156156
invisible(x)
157157
}
158158

@@ -338,9 +338,10 @@ control_bayes <-
338338
}
339339

340340
#' @export
341-
print.control_bayes <- function(x, ...) {
341+
print.control_bayes <- function(x, default = FALSE, ...) {
342342
cli::cli_text("{.emph Bayes control object}")
343-
print_control_settings(x)
343+
defaults <- control_bayes()
344+
print_control_settings(x, default = default, defaults = defaults)
344345
invisible(x)
345346
}
346347

tests/testthat/_snaps/checks.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,165 @@
294294
Warning:
295295
Uncertainty sample scheduled after 5 poor iterations but the search will stop after 2.
296296

297+
# control object print methods
298+
299+
Code
300+
control_grid()
301+
Message
302+
Grid/resamples control object
303+
`verbose`: FALSE
304+
`allow_par`: TRUE
305+
`extract`:
306+
`save_pred`: FALSE
307+
`pkgs`:
308+
`save_workflow`: FALSE
309+
`event_level`: "first"
310+
`parallel_over`:
311+
`backend_options`:
312+
`workflow_size`: 100
313+
314+
---
315+
316+
Code
317+
control_grid(verbose = TRUE, save_pred = TRUE)
318+
Message
319+
Grid/resamples control object
320+
`verbose`: TRUE
321+
`allow_par`: TRUE
322+
`extract`:
323+
`save_pred`: TRUE
324+
`pkgs`:
325+
`save_workflow`: FALSE
326+
`event_level`: "first"
327+
`parallel_over`:
328+
`backend_options`:
329+
`workflow_size`: 100
330+
331+
---
332+
333+
Code
334+
control_grid(pkgs = c("pkg1", "pkg2"), extract = I)
335+
Message
336+
Grid/resamples control object
337+
`verbose`: FALSE
338+
`allow_par`: TRUE
339+
`extract`: <function>
340+
`save_pred`: FALSE
341+
`pkgs`: "pkg1" and "pkg2"
342+
`save_workflow`: FALSE
343+
`event_level`: "first"
344+
`parallel_over`:
345+
`backend_options`:
346+
`workflow_size`: 100
347+
348+
---
349+
350+
Code
351+
control_bayes()
352+
Message
353+
Bayes control object
354+
`verbose`: FALSE
355+
`verbose_iter`: FALSE
356+
`allow_par`: TRUE
357+
`no_improve`: 10
358+
`uncertain`: Inf
359+
`seed`: 51663
360+
`extract`:
361+
`save_pred`: FALSE
362+
`time_limit`: NA
363+
`pkgs`:
364+
`save_workflow`: FALSE
365+
`save_gp_scoring`: FALSE
366+
`event_level`: "first"
367+
`parallel_over`:
368+
`backend_options`:
369+
`workflow_size`: 100
370+
371+
---
372+
373+
Code
374+
control_bayes(verbose_iter = TRUE, no_improve = 5, save_gp_scoring = TRUE)
375+
Message
376+
Bayes control object
377+
`verbose`: FALSE
378+
`verbose_iter`: TRUE
379+
`allow_par`: TRUE
380+
`no_improve`: 5
381+
`uncertain`: Inf
382+
`seed`: 2986
383+
`extract`:
384+
`save_pred`: FALSE
385+
`time_limit`: NA
386+
`pkgs`:
387+
`save_workflow`: FALSE
388+
`save_gp_scoring`: TRUE
389+
`event_level`: "first"
390+
`parallel_over`:
391+
`backend_options`:
392+
`workflow_size`: 100
393+
394+
---
395+
396+
Code
397+
control_last_fit()
398+
Message
399+
Last fit control object
400+
`verbose`: FALSE
401+
`allow_par`: FALSE
402+
`extract`: <function>
403+
`save_pred`: TRUE
404+
`pkgs`:
405+
`save_workflow`: FALSE
406+
`event_level`: "first"
407+
`parallel_over`:
408+
`backend_options`:
409+
`workflow_size`: 100
410+
411+
---
412+
413+
Code
414+
control_last_fit(verbose = TRUE)
415+
Message
416+
Last fit control object
417+
`verbose`: TRUE
418+
`allow_par`: FALSE
419+
`extract`: <function>
420+
`save_pred`: TRUE
421+
`pkgs`:
422+
`save_workflow`: FALSE
423+
`event_level`: "first"
424+
`parallel_over`:
425+
`backend_options`:
426+
`workflow_size`: 100
427+
428+
# control object print methods with default = TRUE
429+
430+
Code
431+
print(control_grid(verbose = TRUE, pkgs = c("pkg1", "pkg2")), default = TRUE)
432+
Message
433+
Grid/resamples control object
434+
`verbose`: TRUE
435+
`pkgs`: "pkg1" and "pkg2"
436+
437+
---
438+
439+
Code
440+
print(control_bayes(verbose_iter = TRUE, no_improve = 5), default = TRUE)
441+
Message
442+
Bayes control object
443+
`verbose_iter`: TRUE
444+
`no_improve`: 5
445+
`seed`: 13797
446+
447+
---
448+
449+
Code
450+
print(control_last_fit(verbose = TRUE), default = TRUE)
451+
Message
452+
Last fit control object
453+
`verbose`: TRUE
454+
`extract`: <function>
455+
297456
# initial values
298457

299458
Code

tests/testthat/test-checks.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,19 @@ test_that("control object print methods", {
415415
expect_snapshot(control_last_fit(verbose = TRUE))
416416
})
417417

418+
test_that("control object print methods with default = TRUE", {
419+
expect_snapshot(print(
420+
control_grid(verbose = TRUE, pkgs = c("pkg1", "pkg2")),
421+
default = TRUE
422+
))
423+
set.seed(456)
424+
expect_snapshot(print(
425+
control_bayes(verbose_iter = TRUE, no_improve = 5),
426+
default = TRUE
427+
))
428+
expect_snapshot(print(control_last_fit(verbose = TRUE), default = TRUE))
429+
})
430+
418431
# ------------------------------------------------------------------------------
419432

420433
test_that("initial values", {

0 commit comments

Comments
 (0)