Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
utterances-bot opened this issue Dec 22, 2023 · 6 comments
Open

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

utterances-bot opened this issue Dec 22, 2023 · 6 comments

Comments

@utterances-bot
Copy link

Cliff notes about the cli package - R-hub blog

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

Copy link

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

maelle commented Dec 22, 2023

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

maelle commented Dec 22, 2023

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

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

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

maelle commented Dec 22, 2023

My question regarding using if to test assertions is likely better explained frictionlessdata/frictionless-r#163 (comment). 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.

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

No branches or pull requests

4 participants