Skip to content

Conversation

@tjburch
Copy link
Contributor

@tjburch tjburch commented Oct 26, 2025

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 and others added 2 commits October 25, 2025 22:04
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@EmilHvitfeldt
Copy link
Member

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
Copy link
Member

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.

@tjburch
Copy link
Contributor Author

tjburch commented Oct 28, 2025

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

@topepo
Copy link
Member

topepo commented Nov 4, 2025

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

expect_s3_class(control_bayes(), "control_bayes")
})

test_that("control object print methods", {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this should live in test-control.R

cli::cli_bullets(c(" " = "{.arg {field}}: <function>"))
} else if (inherits(value, "tune_backend_options")) {
cli::cli_bullets(c(" " = "{.arg {field}}: <backend_options>"))
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a branch for NULL values before the last that is:

 else if (is.null(value)) {
      cli::cli_bullets(c(" " = "{.arg {field}}: NULL"))
    }

We can then get the output without the empty spaces:

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in latest commit

tjburch pushed a commit to tjburch/tune that referenced this pull request Nov 5, 2025
- Add NULL value handling to print_control_settings function
  This prevents empty output for NULL values, showing them explicitly as "NULL"

- Move control object print method tests from test-checks.R to test-control.R
  for better organization

- Remove corresponding snapshots from checks.md (will be regenerated in control.md)

These changes address topepo's review feedback on PR tidymodels#1108.
@tjburch
Copy link
Contributor Author

tjburch commented Nov 9, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better print method for control objects

3 participants