npx skills add https://github.com/posit-dev/skills --skill lifecycle使用 tidyverse 惯例和 lifecycle 包来管理函数和参数的生命周期。
通过检查 man/figures/ 目录中是否存在 lifecycle-*.svg 文件来确认 lifecycle 是否已配置。
如果未配置,请运行:
usethis::use_lifecycle()
此操作将:
Imports 部分添加 lifecycle@importFrom lifecycle deprecatedman/figures/ 目录在 roxygen2 文档中插入徽章:
#' @description
#' `r lifecycle::badge("experimental")`
#' `r lifecycle::badge("deprecated")`
#' `r lifecycle::badge("superseded")`
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
对于参数:
#' @param old_arg `r lifecycle::badge("deprecated")` 请改用 `new_arg`。
仅对生命周期阶段与包整体阶段不同的函数/参数添加徽章。
@description 中添加徽章和说明:#' 执行某些操作
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' `old_fun()` 在 mypkg 1.0.0 版本中已被弃用。请改用 [new_fun()]。
#' @keywords internal
2. 在函数体的第一行添加 deprecate_warn():
old_fun <- function(x) {
lifecycle::deprecate_warn("1.0.0", "old_fun()", "new_fun()")
new_fun(x)
}
3. 在示例中展示迁移方法:
#' @examples
#' old_fun(x)
#' # ->
#' new_fun(x)
| 函数 | 使用时机 |
|---|---|
deprecate_soft() | 第一阶段;仅警告直接用户和在测试期间 |
deprecate_warn() | 标准弃用;每 8 小时警告一次 |
deprecate_stop() | 移除前的最后阶段;提供有用信息的错误 |
主要版本发布的弃用工作流程:
deprecate_stop() - 考虑完全移除函数deprecate_warn() 替换为 deprecate_stop()deprecate_soft() 替换为 deprecate_warn()将实现移至新名称,从旧名称调用并附带弃用警告:
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' 为了保持 API 一致性,`add_two()` 已重命名为 `number_add()`。
#' @keywords internal
#' @export
add_two <- function(x, y) {
lifecycle::deprecate_warn("1.0.0", "add_two()", "number_add()")
number_add(x, y)
}
#' 两个数字相加
#' @export
number_add <- function(x, y) {
x + y
}
使用 deprecated() 作为默认值,并配合 is_present() 检查:
#' @param path `r lifecycle::badge("deprecated")` 请改用 `file`。
write_file <- function(x, file, path = deprecated()) {
if (lifecycle::is_present(path)) {
lifecycle::deprecate_warn("1.4.0", "write_file(path)", "write_file(file)")
file <- path
}
# ... 函数的其余部分
}
add_two <- function(x, y, na_rm = TRUE, na.rm = deprecated()) {
if (lifecycle::is_present(na.rm)) {
lifecycle::deprecate_warn("1.0.0", "add_two(na.rm)", "add_two(na_rm)")
na_rm <- na.rm
}
sum(x, y, na.rm = na_rm)
}
对于有更好替代方案但不应移除的函数:
#' 将列收集为键值对
#'
#' @description
#' `r lifecycle::badge("superseded")`
#'
#' `gather()` 的开发已完成。对于新代码,请使用 [pivot_longer()]。
#'
#' `df %>% gather("key", "value", x, y, z)` 等价于
#' `df %>% pivot_longer(c(x, y, z), names_to = "key", values_to = "value")`。
无需警告 - 只需记录首选的替代方案。
#' @description
#' `r lifecycle::badge("experimental")`
cool_function <- function() {
lifecycle::signal_stage("experimental", "cool_function()")
# ...
}
测试弃用的函数是否正常工作并发出适当的警告:
test_that("old_fun is deprecated", {
expect_snapshot({
x <- old_fun(1)
expect_equal(x, expected_value)
})
})
在现有测试中抑制警告:
test_that("old_fun returns correct value", {
withr::local_options(lifecycle_verbosity = "quiet")
expect_equal(old_fun(1), expected_value)
})
对于影响多个函数的弃用(例如,移除一个公共参数),创建一个内部辅助函数:
warn_for_verbose <- function(
verbose = TRUE,
env = rlang::caller_env(),
user_env = rlang::caller_env(2)
) {
if (!lifecycle::is_present(verbose) || isTRUE(verbose)) {
return(invisible())
}
lifecycle::deprecate_warn(
when = "2.0.0",
what = I("The `verbose` argument"),
details = c(
"Set `options(mypkg_quiet = TRUE)` to suppress messages.",
"The `verbose` argument will be removed in a future release."
),
user_env = user_env
)
invisible()
}
然后在受影响的函数中使用:
my_function <- function(..., verbose = deprecated()) {
warn_for_verbose(verbose)
# ...
}
对于非标准弃用,使用 I() 包装自定义文本:
lifecycle::deprecate_warn(
when = "1.0.0",
what = I('Setting option "pkg.opt" to "foo"'),
with = I('"pkg.new_opt"')
)
what 片段必须能与附加的 "was deprecated in..." 一起使用。
有关详细的生命周期阶段定义和转换,请参阅 references/lifecycle-stages.md。
每周安装次数
97
代码仓库
GitHub 星标数
207
首次出现
2026年2月10日
安全审计
安装于
opencode87
github-copilot86
gemini-cli84
claude-code84
codex84
amp83
Manage function and argument lifecycle using tidyverse conventions and the lifecycle package.
Check if lifecycle is configured by looking for lifecycle-*.svg files in man/figures/.
If not configured, run:
usethis::use_lifecycle()
This:
Imports in DESCRIPTION@importFrom lifecycle deprecated to the package documentation fileman/figures/Insert badges in roxygen2 documentation:
#' @description
#' `r lifecycle::badge("experimental")`
#' `r lifecycle::badge("deprecated")`
#' `r lifecycle::badge("superseded")`
For arguments:
#' @param old_arg `r lifecycle::badge("deprecated")` Use `new_arg` instead.
Only badge functions/arguments whose stage differs from the package's overall stage.
@description:#' Do something
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' `old_fun()` was deprecated in mypkg 1.0.0. Use [new_fun()] instead.
#' @keywords internal
2. Add deprecate_warn() as first line of function body:
old_fun <- function(x) {
lifecycle::deprecate_warn("1.0.0", "old_fun()", "new_fun()")
new_fun(x)
}
3. Show migration in examples:
#' @examples
#' old_fun(x)
#' # ->
#' new_fun(x)
| Function | When to Use |
|---|---|
deprecate_soft() | First stage; warns only direct users and during tests |
deprecate_warn() | Standard deprecation; warns once per 8 hours |
deprecate_stop() | Final stage before removal; errors with helpful message |
Deprecation workflow for major releases:
deprecate_stop() - consider removing function entirelydeprecate_warn() with deprecate_stop()deprecate_soft() with deprecate_warn()Move implementation to new name, call from old name with deprecation:
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' `add_two()` was renamed to `number_add()` for API consistency.
#' @keywords internal
#' @export
add_two <- function(x, y) {
lifecycle::deprecate_warn("1.0.0", "add_two()", "number_add()")
number_add(x, y)
}
#' Add two numbers
#' @export
number_add <- function(x, y) {
x + y
}
Use deprecated() as default value with is_present() check:
#' @param path `r lifecycle::badge("deprecated")` Use `file` instead.
write_file <- function(x, file, path = deprecated()) {
if (lifecycle::is_present(path)) {
lifecycle::deprecate_warn("1.4.0", "write_file(path)", "write_file(file)")
file <- path
}
# ... rest of function
}
add_two <- function(x, y, na_rm = TRUE, na.rm = deprecated()) {
if (lifecycle::is_present(na.rm)) {
lifecycle::deprecate_warn("1.0.0", "add_two(na.rm)", "add_two(na_rm)")
na_rm <- na.rm
}
sum(x, y, na.rm = na_rm)
}
For functions with better alternatives that shouldn't be removed:
#' Gather columns into key-value pairs
#'
#' @description
#' `r lifecycle::badge("superseded")`
#'
#' Development on `gather()` is complete. For new code, use [pivot_longer()].
#'
#' `df %>% gather("key", "value", x, y, z)` is equivalent to
#' `df %>% pivot_longer(c(x, y, z), names_to = "key", values_to = "value")`.
No warning needed - just document the preferred alternative.
#' @description
#' `r lifecycle::badge("experimental")`
cool_function <- function() {
lifecycle::signal_stage("experimental", "cool_function()")
# ...
}
Test that deprecated functions work and warn appropriately:
test_that("old_fun is deprecated", {
expect_snapshot({
x <- old_fun(1)
expect_equal(x, expected_value)
})
})
Suppress warnings in existing tests:
test_that("old_fun returns correct value", {
withr::local_options(lifecycle_verbosity = "quiet")
expect_equal(old_fun(1), expected_value)
})
For deprecations affecting many functions (e.g., removing a common argument), create an internal helper:
warn_for_verbose <- function(
verbose = TRUE,
env = rlang::caller_env(),
user_env = rlang::caller_env(2)
) {
if (!lifecycle::is_present(verbose) || isTRUE(verbose)) {
return(invisible())
}
lifecycle::deprecate_warn(
when = "2.0.0",
what = I("The `verbose` argument"),
details = c(
"Set `options(mypkg_quiet = TRUE)` to suppress messages.",
"The `verbose` argument will be removed in a future release."
),
user_env = user_env
)
invisible()
}
Then use in affected functions:
my_function <- function(..., verbose = deprecated()) {
warn_for_verbose(verbose)
# ...
}
For non-standard deprecations, use I() to wrap custom text:
lifecycle::deprecate_warn(
when = "1.0.0",
what = I('Setting option "pkg.opt" to "foo"'),
with = I('"pkg.new_opt"')
)
The what fragment must work with "was deprecated in..." appended.
See references/lifecycle-stages.md for detailed stage definitions and transitions.
Weekly Installs
97
Repository
GitHub Stars
207
First Seen
Feb 10, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode87
github-copilot86
gemini-cli84
claude-code84
codex84
amp83
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
116,600 周安装