tune icon indicating copy to clipboard operation
tune copied to clipboard

Add print methods for control objects with formatted output using cli

Open tjburch opened this issue 2 months ago • 4 comments

Closes #1051 by adding a cli-based printout of control objects. Went pretty simple, just a cli bulleted list of all parameters. Doing all is up for debate - I figure it's a bit nicer in case users don't know defaults off the cuff, but can see the argument for only non-default values, the prints do get a little verbose.

r$> control_grid()
Grid/resamples control object
  verbose: FALSE
  allow_par: TRUE
  extract: NULL
  save_pred: FALSE
  pkgs: NULL
  save_workflow: FALSE
  event_level: 'first'
  parallel_over: NULL
  backend_options: NULL
  workflow_size: 100
r$> control_bayes(
      verbose_iter = TRUE,
      no_improve = 10,
      uncertain = 5
    )
Bayes control object
  verbose: FALSE
  verbose_iter: TRUE
  allow_par: TRUE
  no_improve: 10
  uncertain: 5
  seed: 9642
  extract: NULL
  save_pred: FALSE
  time_limit: NA
  pkgs: NULL
  save_workflow: FALSE
  save_gp_scoring: FALSE
  event_level: 'first'
  parallel_over: NULL
  backend_options: NULL
  workflow_size: 100
$> control_last_fit(
      verbose = TRUE,
      event_level = "second",
      allow_par = TRUE
    )
Last fit control object
  verbose: TRUE
  allow_par: TRUE
  extract: <function>
  save_pred: TRUE
  pkgs: NULL
  save_workflow: FALSE
  event_level: 'second'
  parallel_over: NULL
  backend_options: NULL
  workflow_size: 100

tjburch avatar Oct 26 '25 02:10 tjburch

This is all nice already! I would suggest, if you have time to spare, to take a look at the cli classes we can use. This should allow you to reduce some of the code you have in print_control_settings() as well as give using some neat formatting of the values.

EmilHvitfeldt avatar Oct 27 '25 23:10 EmilHvitfeldt

Additional if we wanted to only show what is non-default we could add a default argument to print_control_settings().

Add the following line inside the loop

    if (identical(value, default[[field]])) {
      next
    }

and lastly pass in the corresponding objects in the uses. So print.control_grid would be

print.control_grid <- function(x, ...) {
  cli::cli_text("{.emph Grid/resamples control object}")
---  print_control_settings(x)
+++  print_control_settings(x, default = control_grid())
  invisible(x)
}

I'm looping @topepo in on this decision as i don't have very strong feelings here.

EmilHvitfeldt avatar Oct 27 '25 23:10 EmilHvitfeldt

Thanks - implemented. The function print is still kind of ugly, but the rest now use the cli classes better.

tjburch avatar Oct 28 '25 10:10 tjburch

I thought that we could do something fancy with formals() but this works nicely.

topepo avatar Nov 04 '25 18:11 topepo

Cool, just implemented NULL handling in the latest commit and put tests in the right place. Let me know if there's anything else.

Ignore the claude reference above - got some free credits dumped into my account that I figured I'd see what it could do and didn't realize it would go off and put its changes on git.

tjburch avatar Nov 09 '25 02:11 tjburch