Skip to content

Using cli functions for logging to console splits output chunk #2606

Open

Description

@cderv

This is something I noticed while looking at output on a blog post: https://tidyverse.org/blog/2025/11/two-new-tidymodels-packages/#important

Reproducible with

---
title: "testing"
output: 
  html_document:
    keep_md: true
---

```{r}
library(tidymodels)
library(filtro)

set.seed(1)
yield_split <- initial_split(modeldata::chem_proc_yield)
yield_split
yield_train <- training(yield_split)
yield_test <- testing(yield_split)

``` 


```{r}
library(important)
library(desirability2)
goals <-
  desirability(
    maximize(cor_spearman, low = 0.25, high = 1),
    maximize(imp_rf_oblique, scale = 2)
  )

yield_rec <-
  recipe(yield ~ ., data = yield_train) |>
  step_impute_knn(all_predictors(), neighbors = 10) |>
  step_predictor_desirability(
    all_predictors(),
    score = goals,
    prop_terms = 1 / 10
  )
yield_rec
```

Looking at recipes:::print.recipe(), it is using cli only
https://github.com/tidymodels/recipes/blob/810b5b55b912dcae8c57f6d0a8d4db75ca88f7cd/R/recipe.R#L875-L927

So we can reproduce using something like below

---
title: "testing"
output: 
  html_document:
    keep_md: true
---

```{r}
print_cli <- function() {
  cli::cli_div(theme = list(.pkg = list(`vec-trunc` = Inf, 
    `vec-last` = ", ")))
  cli::cli_h1("Recipe")
  cli::cli_h3("Inputs")
  cli::cli_text("Number of variables by role")
  tab <- c("X", "Y")
  values <- 1:2
  cli::cli_verbatim(glue::glue("{names(tab)}: {values}"))
  cli::cli_end()
}
print_cli()
```
Image

This is because we have this intermediate md

---
title: "testing"
output: 
  html_document:
    keep_md: true
---


``` r
print_cli <- function() {
  cli::cli_div(theme = list(.pkg = list(`vec-trunc` = Inf, 
    `vec-last` = ", ")))
  cli::cli_h1("Recipe")
  cli::cli_h3("Inputs")
  cli::cli_text("Number of variables by role")
  tab <- c("X", "Y")
  values <- 1:2
  cli::cli_verbatim(glue::glue("{names(tab)}: {values}"))
  cli::cli_end()
}
print_cli()
```

```
## 
```

```
## ── Recipe ──────────────────────────────────────────────────────────────────────
```

```
## 
```

```
## ── Inputs
```

```
## Number of variables by role
```

```
## 
```

Is this something we should try to improve in knitr or rmarkdown ? Or is this something related to cli usage ?

I'll look into cli logic to be sure, but maybe you gave ideas @yihui

Activity

  1. yihui commented on Nov 25, 2025

    @yihui
    Collaborator

    I'm not sure where these newlines came from or why the text output was split into separate blocks. Personally I'd just disable cli's ANSI sequences for pure text output, e.g., https://github.com/yihui/xfun/blob/1188b7c/R/record.R#L244-L245 but it doesn't seem to help here (I just tried it).

  2. pinned this issue on Dec 23, 2025
  3. unpinned this issue on Dec 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions