rlang-conditions
БесплатноБез исполняемых скриптовНе проверенUse when developing R packages that need to handle errors, warnings, and conditions properly. Covers: (1) cli_abort/cli_warn/cli_inform for throwing…
от jsperger★ 17 звёзд на GitHubОб этом скилле
Condition Handling in R Packages
When to Use What
| Task | Use |
|---|---|
| Throw formatted error | cli_abort() with message and bullets |
| Throw formatted warning | cli_warn() with message and bullets |
| Display informative message | cli_inform() with message and bullets |
| Show correct function in error | call = caller_env() argument |
| Get argument name dynamically | arg = caller_arg(x) in input checkers |
| Catch and rethrow with context | try_fetch() with parent = cnd |
| Take ownership of low-level error | try_fetch() with parent = NA |
| Test error messages | expect_snapshot(error = TRUE, ...) |
CLI Conditions Essentials
Use cli_abort(), cli_warn(), and cli_inform() instead of base R's stop(), warning(), and message() for formatted condition messages.
Basic Usage
# Simple error
cli_abort("Column {.field name} is required")
# Simple warning
cli_warn("Column {.field {col}} has missing values")
# Simple message
cli_inform("Processing {n} file{?s}")
Structured Messages with Bullets
Use named character vectors for multi-part messages:
cli_abort(c(
"File not found",
"x" = "Cannot read {.file {path}}",
"i" = "Check that the file exists"
))
Bullet types:
"x"- Error/problem (red X)"!"- Warning (yellow !)"i"- Information (blue i)"v"- Success (green checkmark)"*"- Bullet point">"- Arrow/pointer
Essential Inline Markup
Format values in condition messages for clarity:
# Arguments
cli_abort("{.arg x} must be numeric")
# Files and paths
cli_abort("Cannot find {.file {path}}")
# Values
cli_abort("Expected {.val TRUE}, got {.val {x}}")
# Classes/types
cli_abort("Expected numeric, got {.cls {class(x)}}")
# Code
cli_abort("Use {.code na.rm = TRUE} to ignore missing values
")
# Function names
cli_abort("See {.fn mypackage::helper} for details")
Pluralization
Use {?} for automatic singular/plural handling:
cli_abort("Found {n} error{?s}")
#> Found 1 error
#> Found 3 errors
cli_abort("Found {n} director{?y/ies}")
#> Found 1 directory
#> Found 5 directories
Error Call Context
By default, abort() and cli_abort() show the function where they are called. When using error helpers, pass call = caller_env() to show the user's function instead.
The Problem
# Error helper without call context
stop_my_class <- function(message) {
abort(message, class = "my_class")
}
my_function <- function(x) {
stop_my_class("Something went wrong")
}
my_function("test")
#> Error in `stop_my_class()`:
#> ! Something went wrong
The error shows stop_my_class() but users called my_function().
The Solution
stop_my_class <- function(message, call = caller_env()) {
abort(message, class = "my_class", call = call)
}
my_function <- function(x) {
stop_my_class("Something went wrong")
}
my_function("test")
#> Error in `my_function()`:
#> ! Something went wrong
Now the error correctly shows my_function().
Pattern: Abort Wrapper
stop_my_class <- function(message, call = caller_env()) {
abort(message, class = "my_class", call = call)
}
Input Checkers
Input checkers validate function arguments. Use caller_arg() to get the argument name dynamically and caller_env() for proper error context.
Basic Input Checker
check_string <- function(x,
arg = caller_arg(x),
call = caller_env()) {
if (!is_string(x)) {
cli_abort("{.arg {arg}} must be a string.", call = call)
}
}
Using the Checker
my_function <- function(my_arg) {
check_string(my_arg)
# ... rest of function
}
my_function(NA)
#> Error in `my_function()`:
#> ! `my_arg` must be a string.
The error shows:
- The correct function (
my_function, notcheck_string) - The correct argument name (
my_arg, notx)
Pattern: Complete Input Checker
check_scalar_numeric <- function(x,
arg = caller_arg(x),
call = caller_env()) {
if (!is.numeric(x) || length(x) != 1) {
cli_abort(c(
"{.arg {arg}} must be a single number",
"x" = "You supplied {.cls {class(x)}} of length {length(x)}"
), call = call)
}
}
Built-in Checkers from rlang
rlang provides check_required() for mandatory arguments:
my_function <- function(data) {
check_required(data)
# ... rest of function
}
my_function()
#> Error in `my_function()`:
#> ! `data` is absent but must be supplied.
Error Chaining
Error chaining adds context when catching and rethrowing errors. Use try_fetch() to catch errors and parent to chain them.
Basic Error Chaining
with_chained_errors <- function(expr, call = caller_env()) {
try_fetch(
expr,
error = function(cnd) {
abort("Problem during step.", parent = cnd, call = call)
}
)
}
my_verb <- function(expr) {
with_chained_error
Установить rlang-conditions в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
FAQ
Что делает скилл rlang-conditions?
Use when developing R packages that need to handle errors, warnings, and conditions properly. Covers: (1) cli_abort/cli_warn/cli_inform for throwing conditions with formatting, (2) Error call context with caller_env() and caller_arg(), (3) Input validation helpers that report the right function, (4) Error chaining with try_fetch() and parent argument, (5) Testing error conditions with testthat snapshots.
Как установить скилл rlang-conditions?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл rlang-conditions запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
docx
Use this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include…
от AnthropicUse this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple…
от Anthropicxlsx
Use this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing…
от Anthropicpptx
Use this skill any time a .pptx or .potx file is involved in any way — as input, output, or both. This includes: creating slide decks, pitch decks, or…
от Anthropic