r-hub / blog

The R-hub blog
https://blog.r-hub.io/
11 stars 12 forks source link

Cliff notes about the cli package - R-hub blog #169

Open utterances-bot opened 9 months ago

utterances-bot commented 9 months ago

Cliff notes about the cli package - R-hub blog

https://blog.r-hub.io/2023/11/30/cliff-notes-about-cli/

peterdesmet commented 9 months ago

I'm just starting with cli, so this post is excellent! I have two questions:

  1. Can you explain why you need rlang as a dependency if you use cli_abort()? What happens if you don't add it. Also, what is the guidance on cli_abort()? Do you wrap it in an if condition to test an assertion?

  2. Any advise on how to wrap long CLI text to 80 chars in your code (styler guideline), but not in the console. I notice paste() works (and respects the glue syntax):

cli::cli_alert(paste(
  "This {.val text} is longer than 80 characters in my code, so I wrap it with",
  "{.fun paste}."
))
maelle commented 9 months ago

:wave: @peterdesmet! Thanks for your comment. Cc @drmowinckels

  1. You get an error (unless rlang has been installed via some other dependencies). cli_abort() unconditionally calls rlang::abort() https://github.com/r-lib/cli/blob/e7a62defc0442afba235042d29b3e608d5345831/R/rlang.R#L45

  2. Not sure what best practice is but it also works with c()

cli::cli_alert(c(
  "This {.val text} is longer than 80 characters in my code, so I wrap it with",
  "{.fun paste}."
))
#> → This "text" is longer than 80 characters in my code, so I wrap it with`paste()`.

Created on 2023-12-22 with reprex v2.0.2

maelle commented 9 months ago

Do you wrap it in an if condition to test an assertion?

I'm not sure I understand the question. Does https://www.njtierney.com/post/2023/12/06/long-errors-smell/ help at all?

peterdesmet commented 9 months ago
  1. My question regarding using if to test assertions is likely better explained here. Thanks for the Code Smell article, will read that.
  2. While c() works in cli_alert(), it generates multiple lines in e.g. cli_abort(). So paste() to the rescue:
library(cli)
cli::cli_abort(c(
  "This {.val text} is longer than 80 characters in my code, so I wrap it with",
  "{.fun paste}."
))
#> Error:
#> ! This "text" is longer than 80 characters in my code, so I wrap it with
#> `paste()`.
#> Backtrace:
#>     ▆
#>  1. └─cli::cli_abort(...)
#>  2.   └─rlang::abort(...) at cli/R/rlang.R:45:3
cli::cli_abort(paste(
  "This {.val text} is longer than 80 characters in my code, so I wrap it with",
  "{.fun paste}."
))
#> Error:
#> ! This "text" is longer than 80 characters in my code, so I wrap it with
#>   `paste()`.
#> Backtrace:
#>     ▆
#>  1. └─cli::cli_abort(...)
#>  2.   └─rlang::abort(...) at cli/R/rlang.R:45:3

Created on 2023-12-22 with reprex v2.0.2

gaborcsardi commented 9 months ago

@peterdesmet you can also write a multi-line string instead of paste(), line breaks are ignored:

cli::cli_alert(
  "This {.val text} is longer than 80 characters in my code, so I just write it in
   two lines, no need to use {.fun paste}."
)
maelle commented 9 months ago

My question regarding using if to test assertions is likely better explained https://github.com/frictionlessdata/frictionless-r/issues/163#issuecomment-1865897243. Thanks for the Code Smell article, will read that.

Ah yes then the blog post will be relevant for sure as it discusses the creation of helper functions to check arguments.

Also I see https://vctrs.r-lib.org/reference/vec_assert.html but it has a "questioning" lifecycle badge.