npx skills add https://github.com/posit-dev/skills --skill cli任务:显示带有上下文和格式的错误信息
使用:cli_abort(),配合行内标记和项目符号列表
任务:显示带格式的警告信息
使用:cli_warn(),配合行内标记
任务:显示信息性消息
使用:cli_inform(),配合行内标记
任务:显示计数操作的进度
使用:cli_progress_bar(),配合总数
任务:显示简单的进度步骤
使用:cli_progress_step(),配合状态消息
任务:格式化代码或函数名
使用:{.code ...} 或 {.fn package::function}
任务:格式化文件路径
使用:{.file path/to/file}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
任务:格式化包名
使用:{.pkg packagename}
任务:格式化变量名
使用:{.var variable_name}
任务:格式化值
使用:{.val value}
任务:处理单数/复数文本
使用:{?s} 或 {?y/ies},配合复数形式
任务:创建标题
使用:cli_h1(), cli_h2(), cli_h3()
任务:创建警报
使用:cli_alert_success(), cli_alert_danger(), cli_alert_warning(), cli_alert_info()
任务:创建列表
使用:cli_ul(), cli_ol(), cli_dl(),配合 cli_li()
使用 {.class content} 语法的行内标记来格式化文本:
# 基本格式化
cli_text("函数 {.fn mean} 计算平均值")
cli_text("安装包 {.pkg dplyr}")
cli_text("查看文件 {.file ~/.Rprofile}")
cli_text("{.var x} 必须是数值型,而不是 {.obj_type_of {x}}")
cli_text("获取值 {.val {x}}"))
# 代码格式化
cli_text("使用 {.code sum(x, na.rm = TRUE)}")
# 路径和参数
cli_text("从 {.path /data/file.csv} 读取")
cli_text("将 {.arg na.rm} 设为 TRUE")
# 类型和类
cli_text("对象是 {.cls data.frame}")
# 强调
cli_text("这是 {.emph 重要的}")
cli_text("这是 {.strong 关键的}")
# 字段
cli_text("{.field name} 字段是必需的")
向量会自动用逗号和“和”进行折叠:
pkgs <- c("dplyr", "tidyr", "ggplot2")
cli_text("正在安装包:{.pkg {pkgs}}")
#> 正在安装包:dplyr、tidyr 和 ggplot2
files <- c("data.csv", "script.R")
cli_text("找到 {length(files)} 个文件{?s}:{.file {files}}")
#> 找到 2 个文件:data.csv 和 script.R
使用双花括号 {{ 和 }} 来转义字面量花括号:
cli_text("在 glue 中使用 {{variable}} 语法")
#> 在 glue 中使用 {variable} 语法
完整标记参考:查看 references/inline-markup.md 以获取所有 50 多个行内类、边界情况、嵌套规则和高级模式。
使用 {?} 进行复数化,有三种模式:
nfile <- 1
cli_text("找到 {nfile} 个文件{?s}")
#> 找到 1 个文件
nfile <- 3
cli_text("找到 {nfile} 个文件{?s}")
#> 找到 3 个文件
ndir <- 1
cli_text("找到 {ndir} 个目录{?y/ies}")
#> 找到 1 个目录
ndir <- 5
cli_text("找到 {ndir} 个目录{?y/ies}")
#> 找到 5 个目录
nfile <- 0
cli_text("找到 {nfile} 个文件{?s}:{?no/the/the} 文件{?s}")
#> 找到 0 个文件:没有文件
nfile <- 1
cli_text("找到 {nfile} 个文件{?s}:{?no/the/the} 文件{?s}")
#> 找到 1 个文件:该文件
nfile <- 3
cli_text("找到 {nfile} 个文件{?s}:{?no/the/the} 文件{?s}")
#> 找到 3 个文件:这些文件
使用 no() 来显示“没有”而不是零:
nfile <- 0
cli_text("找到 {no(nfile)} 个文件{?s}")
#> 找到没有文件
使用 qty() 来显式设置数量:
nupd <- 3
ntotal <- 10
cli_text("{nupd}/{ntotal} {qty(nupd)} 个文件{?s} {?needs/need} 更新")
#> 3/10 个文件需要更新
高级复数形式:查看 references/inline-markup.md 以获取边界情况和复杂模式。
使用 cli 条件替代基础 R,以获得更好的格式化效果:
# 之前(基础 R)
stop("文件未找到:", path)
# 之后(cli)
cli_abort("文件 {.file {path}} 未找到")
# 带项目符号以提供上下文
check_file <- function(path) {
if (!file.exists(path)) {
cli_abort(c(
"文件未找到",
"x" = "无法读取 {.file {path}}",
"i" = "检查文件是否存在"
))
}
}
# 之前(基础 R)
warning("列 ", col, " 有缺失值")
# 之后(cli)
cli_warn("列 {.field {col}} 有缺失值")
# 带上下文
cli_warn(c(
"检测到数据质量问题",
"!" = "列 {.field {col}} 有 {n_missing} 个缺失值{?s}",
"i" = "考虑使用 {.fn tidyr::drop_na}"
))
# 之前(基础 R)
message("正在处理 ", n, " 个文件")
# 之后(cli)
cli_inform("正在处理 {n} 个文件{?s}")
# 带结构
cli_inform(c(
"v" = "成功加载 {.pkg dplyr}",
"i" = "版本 {packageVersion('dplyr')}"
))
"x" - 错误/问题(红色 X)"!" - 警告(黄色 !)"i" - 信息(蓝色 i)"v" - 成功(绿色对勾)"*" - 项目符号点">" - 箭头/指针高级错误设计:查看 references/conditions.md 以获取错误设计原则、rlang 集成、测试策略和真实世界模式。
process_data <- function() {
cli_progress_step("正在加载数据")
data <- load_data()
cli_progress_step("正在清理数据")
clean <- clean_data(data)
cli_progress_step("正在分析数据")
analyze(clean)
}
process_files <- function(files) {
cli_progress_bar("正在处理文件", total = length(files))
for (file in files) {
process_file(file)
cli_progress_update()
}
}
进度条在函数退出时自动关闭:
process <- function() {
cli_progress_bar("正在工作", total = 100)
for (i in 1:100) {
Sys.sleep(0.01)
cli_progress_update()
}
# 无需调用 cli_progress_done() - 自动关闭
}
高级进度:查看 references/progress.md 以获取嵌套进度、自定义格式、并行处理、所有进度变量和 Shiny 集成。
cli_h1("主部分")
cli_h2("子部分")
cli_h3("细节")
cli_alert_success("操作成功完成")
cli_alert_danger("发生严重错误")
cli_alert_warning("检测到潜在问题")
cli_alert_info("有额外信息可用")
# 带标记的常规文本
cli_text("这是带 {.emph 强调} 的格式化文本")
# 代码块
cli_code(c(
"library(dplyr)",
"mtcars %>% filter(mpg > 20)"
))
# 逐字文本(无格式化)
cli_verbatim("这完全按原样显示:{not interpolated}")
# 无序列表
cli_ul()
cli_li("第一项")
cli_li("第二项")
cli_end()
# 有序列表
cli_ol()
cli_li("第一步")
cli_li("第二步")
cli_end()
# 定义列表
cli_dl()
cli_li(c(name = "名称字段"))
cli_li(c(email = "电子邮件地址"))
cli_end()
# 之前:基础 R 错误处理
validate_input <- function(x, y) {
if (!is.numeric(x)) {
stop("x 必须是数值型")
}
if (length(y) == 0) {
stop("y 不能为空")
}
if (length(x) != length(y)) {
stop("x 和 y 必须长度相同")
}
}
# 之后:CLI 错误处理
validate_input <- function(x, y) {
if (!is.numeric(x)) {
cli_abort(c(
"{.arg x} 必须是数值型",
"x" = "你提供了一个 {.cls {class(x)}} 向量",
"i" = "使用 {.fn as.numeric} 进行转换"
))
}
if (length(y) == 0) {
cli_abort(c(
"{.arg y} 不能为空",
"i" = "提供至少一个元素"
))
}
if (length(x) != length(y)) {
cli_abort(c(
"{.arg x} 和 {.arg y} 必须长度相同",
"x" = "{.arg x} 的长度为 {length(x)}",
"x" = "{.arg y} 的长度为 {length(y)}"
))
}
}
check_required_columns <- function(data, required_cols) {
actual_cols <- names(data)
missing_cols <- setdiff(required_cols, actual_cols)
if (length(missing_cols) > 0) {
cli_abort(c(
"数据中缺少必需的列{?s}",
"x" = "缺少 {length(missing_cols)} 列{?s}:{.field {missing_cols}}",
"i" = "数据有 {length(actual_cols)} 列{?s}:{.field {actual_cols}}",
"i" = "添加缺少的列{?s}或检查拼写错误"
))
}
invisible(data)
}
process_files <- function(files, verbose = TRUE) {
n <- length(files)
if (verbose) {
cli_progress_bar(
format = "正在处理 {cli::pb_bar} {cli::pb_current}/{cli::pb_total} [{cli::pb_eta}]",
total = n
)
}
results <- vector("list", n)
for (i in seq_along(files)) {
results[[i]] <- process_file(files[[i]])
if (verbose) {
cli_progress_update()
}
}
results
}
references/inline-markup.md - 按类别组织的完整行内类目录、高级模式、嵌套规则和真实世界示例
references/conditions.md - 高级错误设计模式、rlang 集成、使用 testthat 快照测试、迁移指南和反模式
references/progress.md - 嵌套进度条、自定义格式、所有进度变量、并行处理、Shiny 集成和调试
references/themes.md - 完整的主题系统,包含类似 CSS 的选择器、容器函数、调色板、自定义主题和可访问性
references/ansi-operations.md - ANSI 字符串操作(对齐、列、nchar 等)、超链接、颜色检测、测试 CLI 输出和故障排除
{} 语法提供支持每周安装次数
103
仓库
GitHub 星标数
207
首次出现
2026年2月10日
安全审计
安装于
opencode93
github-copilot91
codex90
gemini-cli89
amp88
kimi-cli88
task: Display error with context and formatting use: cli_abort() with inline markup and bullet lists
task: Show warning with formatting use: cli_warn() with inline markup
task: Display informative message use: cli_inform() with inline markup
task: Show progress for counted operations use: cli_progress_bar() with total count
task: Show simple progress steps use: cli_progress_step() with status messages
task: Format code or function names use: {.code ...} or {.fn package::function}
task: Format file paths use: {.file path/to/file}
task: Format package names use: {.pkg packagename}
task: Format variable names use: {.var variable_name}
task: Format values use: {.val value}
task: Handle singular/plural text use: {?s} or {?y/ies} with pluralization
task: Create headers use: cli_h1(), cli_h2(), cli_h3()
task: Create alerts use: cli_alert_success(), cli_alert_danger(), cli_alert_warning(), cli_alert_info()
task: Create lists use: cli_ul(), cli_ol(), cli_dl() with cli_li()
Use inline markup with {.class content} syntax to format text:
# Basic formatting
cli_text("Function {.fn mean} calculates averages")
cli_text("Install package {.pkg dplyr}")
cli_text("See file {.file ~/.Rprofile}")
cli_text("{.var x} must be numeric, not {.obj_type_of {x}}")
cli_text("Got value {.val {x}}"))
# Code formatting
cli_text("Use {.code sum(x, na.rm = TRUE)}")
# Paths and arguments
cli_text("Reading from {.path /data/file.csv}")
cli_text("Set {.arg na.rm} to TRUE")
# Types and classes
cli_text("Object is {.cls data.frame}")
# Emphasis
cli_text("This is {.emph important}")
cli_text("This is {.strong critical}")
# Fields
cli_text("The {.field name} field is required")
Vectors are automatically collapsed with commas and "and":
pkgs <- c("dplyr", "tidyr", "ggplot2")
cli_text("Installing packages: {.pkg {pkgs}}")
#> Installing packages: dplyr, tidyr, and ggplot2
files <- c("data.csv", "script.R")
cli_text("Found {length(files)} file{?s}: {.file {files}}")
#> Found 2 files: data.csv and script.R
Use double braces {{ and }} to escape literal braces:
cli_text("Use {{variable}} syntax in glue")
#> Use {variable} syntax in glue
For complete markup reference : See references/inline-markup.md for all 50+ inline classes, edge cases, nesting rules, and advanced patterns.
Use {?} for pluralization with three patterns:
nfile <- 1
cli_text("Found {nfile} file{?s}")
#> Found 1 file
nfile <- 3
cli_text("Found {nfile} file{?s}")
#> Found 3 files
ndir <- 1
cli_text("Found {ndir} director{?y/ies}")
#> Found 1 directory
ndir <- 5
cli_text("Found {ndir} director{?y/ies}")
#> Found 5 directories
nfile <- 0
cli_text("Found {nfile} file{?s}: {?no/the/the} file{?s}")
#> Found 0 files: no files
nfile <- 1
cli_text("Found {nfile} file{?s}: {?no/the/the} file{?s}")
#> Found 1 file: the file
nfile <- 3
cli_text("Found {nfile} file{?s}: {?no/the/the} file{?s}")
#> Found 3 files: the files
Use no() to display "no" instead of zero:
nfile <- 0
cli_text("Found {no(nfile)} file{?s}")
#> Found no files
Use qty() to set quantity explicitly:
nupd <- 3
ntotal <- 10
cli_text("{nupd}/{ntotal} {qty(nupd)} file{?s} {?needs/need} updates")
#> 3/10 files need updates
For advanced pluralization : See references/inline-markup.md for edge cases and complex patterns.
Use cli conditions instead of base R for better formatting:
# Before (base R)
stop("File not found: ", path)
# After (cli)
cli_abort("File {.file {path}} not found")
# With bullets for context
check_file <- function(path) {
if (!file.exists(path)) {
cli_abort(c(
"File not found",
"x" = "Cannot read {.file {path}}",
"i" = "Check that the file exists"
))
}
}
# Before (base R)
warning("Column ", col, " has missing values")
# After (cli)
cli_warn("Column {.field {col}} has missing values")
# With context
cli_warn(c(
"Data quality issues detected",
"!" = "Column {.field {col}} has {n_missing} missing value{?s}",
"i" = "Consider using {.fn tidyr::drop_na}"
))
# Before (base R)
message("Processing ", n, " files")
# After (cli)
cli_inform("Processing {n} file{?s}")
# With structure
cli_inform(c(
"v" = "Successfully loaded {.pkg dplyr}",
"i" = "Version {packageVersion('dplyr')}"
))
"x" - Error/problem (red X)"!" - Warning (yellow !)"i" - Information (blue i)"v" - Success (green checkmark)"*" - Bullet point">" - Arrow/pointerFor advanced error design : See references/conditions.md for error design principles, rlang integration, testing strategies, and real-world patterns.
process_data <- function() {
cli_progress_step("Loading data")
data <- load_data()
cli_progress_step("Cleaning data")
clean <- clean_data(data)
cli_progress_step("Analyzing data")
analyze(clean)
}
process_files <- function(files) {
cli_progress_bar("Processing files", total = length(files))
for (file in files) {
process_file(file)
cli_progress_update()
}
}
Progress bars auto-close when the function exits:
process <- function() {
cli_progress_bar("Working", total = 100)
for (i in 1:100) {
Sys.sleep(0.01)
cli_progress_update()
}
# No need to call cli_progress_done() - auto-closes
}
For advanced progress : See references/progress.md for nested progress, custom formats, parallel processing, all progress variables, and Shiny integration.
cli_h1("Main Section")
cli_h2("Subsection")
cli_h3("Detail")
cli_alert_success("Operation completed successfully")
cli_alert_danger("Critical error occurred")
cli_alert_warning("Potential issue detected")
cli_alert_info("Additional information available")
# Regular text with markup
cli_text("This is formatted text with {.emph emphasis}")
# Code blocks
cli_code(c(
"library(dplyr)",
"mtcars %>% filter(mpg > 20)"
))
# Verbatim text (no formatting)
cli_verbatim("This is displayed exactly as-is: {not interpolated}")
# Unordered list
cli_ul()
cli_li("First item")
cli_li("Second item")
cli_end()
# Ordered list
cli_ol()
cli_li("First step")
cli_li("Second step")
cli_end()
# Definition list
cli_dl()
cli_li(c(name = "The name field"))
cli_li(c(email = "The email address"))
cli_end()
# Before: Base R error handling
validate_input <- function(x, y) {
if (!is.numeric(x)) {
stop("x must be numeric")
}
if (length(y) == 0) {
stop("y cannot be empty")
}
if (length(x) != length(y)) {
stop("x and y must have the same length")
}
}
# After: CLI error handling
validate_input <- function(x, y) {
if (!is.numeric(x)) {
cli_abort(c(
"{.arg x} must be numeric",
"x" = "You supplied a {.cls {class(x)}} vector",
"i" = "Use {.fn as.numeric} to convert"
))
}
if (length(y) == 0) {
cli_abort(c(
"{.arg y} cannot be empty",
"i" = "Provide at least one element"
))
}
if (length(x) != length(y)) {
cli_abort(c(
"{.arg x} and {.arg y} must have the same length",
"x" = "{.arg x} has length {length(x)}",
"x" = "{.arg y} has length {length(y)}"
))
}
}
check_required_columns <- function(data, required_cols) {
actual_cols <- names(data)
missing_cols <- setdiff(required_cols, actual_cols)
if (length(missing_cols) > 0) {
cli_abort(c(
"Required column{?s} missing from data",
"x" = "Missing {length(missing_cols)} column{?s}: {.field {missing_cols}}",
"i" = "Data has {length(actual_cols)} column{?s}: {.field {actual_cols}}",
"i" = "Add the missing column{?s} or check for typos"
))
}
invisible(data)
}
process_files <- function(files, verbose = TRUE) {
n <- length(files)
if (verbose) {
cli_progress_bar(
format = "Processing {cli::pb_bar} {cli::pb_current}/{cli::pb_total} [{cli::pb_eta}]",
total = n
)
}
results <- vector("list", n)
for (i in seq_along(files)) {
results[[i]] <- process_file(files[[i]])
if (verbose) {
cli_progress_update()
}
}
results
}
references/inline-markup.md - Complete catalog of inline classes organized by category, advanced patterns, nesting rules, and real-world examples
references/conditions.md - Advanced error design patterns, rlang integration, testing with testthat snapshots, migration guide, and anti-patterns
references/progress.md - Nested progress bars, custom formats, all progress variables, parallel processing, Shiny integration, and debugging
references/themes.md - Complete theming system with CSS-like selectors, container functions, color palettes, custom themes, and accessibility
references/ansi-operations.md - ANSI string operations (align, columns, nchar, etc.), hyperlinks, color detection, testing CLI output, and troubleshooting
{} syntaxWeekly Installs
103
Repository
GitHub Stars
207
First Seen
Feb 10, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode93
github-copilot91
codex90
gemini-cli89
amp88
kimi-cli88
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
120,000 周安装
React Three Fiber物理引擎教程 - r3f-physics(Rapier)快速入门与实战
348 周安装
Implement技能:端到端问题执行与代码变更自动化工作流指南
339 周安装
SQLAlchemy Alembic 专家最佳实践与代码审查指南 - 生产级数据库迁移优化
344 周安装
React Three Fiber材质教程:PBR材质、meshStandardMaterial、meshPhysicalMaterial详解
346 周安装
rpi:完整的RPI生命周期编排器,自动化软件开发生命周期管理工具
348 周安装
自我改进智能体:AI智能体自我优化与技能创建指南
346 周安装