R Package Development

posit-dev/skills/r-lib/r-package-development

作者 posit-deve20b71b2ab527b7c480f4f60dcce2b18df1a99ae無授權條款收錄於 2026年10月9日更新於 2026年10月9日

R package development with devtools, testthat, and roxygen2. Use when the user is working on an R package, running tests, writing documentation, or building package infrastructure.

AI 產生的概覽

使用 devtools、testthat 和 roxygen2 開發 R 套件的規範與指令。

功能
提供一組 R 指令,用於載入套件程式碼、執行測試、重新產生文件、檢查 pkgdown 網站以及執行 R CMD check。它也說明程式撰寫、測試、文件與 NEWS.md 的慣例,包括測試檔案位置、快照斷言、roxygen 換行和更新日誌項目排序。其產出是套用於現有或新建 R 套件的指引,而不是產生的檔案。
適用情境
在開發 R 套件並需要執行測試、撰寫函式文件、檢查套件或遵循專案慣例時使用。在為套件新增程式碼、測試、roxygen 文件或 NEWS.md 項目時也適用。
執行需求
需要 R 執行環境以及 devtools、testthat、roxygen2 和 pkgdown 套件,還需要 air 格式化工具。此技能不附帶指令碼,僅提供說明。

R package development

Key commands

# Run code in the packageRscript -e "devtools::load_all(); code"
# Run all testsRscript -e "devtools::test()"
# Run all tests for files starting with {name}Rscript -e "devtools::test(filter = '^{name}')"
# Run all tests for R/{name}.RRscript -e "devtools::test_active_file('R/{name}.R')"
# Run a single test "blah" for R/{name}.RRscript -e "devtools::test_active_file('R/{name}.R', desc = 'blah')"
# Redocument the packageRscript -e "devtools::document()"
# Check pkgdown documentationRscript -e "pkgdown::check_pkgdown()"
# Check the package with R CMD checkRscript -e "devtools::check()"
# Format codeair format .

Coding

  • Always run air format . after generating code.
  • Use the base pipe operator (|>) not the magrittr pipe (%>%).
  • Use \() ... for single-line anonymous functions. For all other cases, use function() {...}.

Testing

  • Tests for R/{name}.R go in tests/testthat/test-{name}.R.
  • All new code should have an accompanying test.
  • If there are existing tests, place new tests next to similar existing tests.
  • Strive to keep tests minimal with few comments.
  • Avoid expect_true() and expect_false() in favour of a specific expectation which will give a better failure message.
  • When testing errors and warnings, don't use expect_error() or expect_warning(). Instead, use expect_snapshot(error = TRUE) for errors and expect_snapshot() for warnings because these allow the user to review the full text of the output.

Documentation

  • Every user-facing function should be exported and have roxygen2 documentation.
  • Wrap roxygen comments at 80 characters.
  • Internal functions should not have roxygen documentation.
  • Whenever you add a new (non-internal) documentation topic, also add the topic to _pkgdown.yml.
  • Always re-document the package after changing a roxygen2 comment.
  • Use pkgdown::check_pkgdown() to check that all topics are included in the reference index.

NEWS.md

  • Every user-facing change should be given a bullet in NEWS.md. Do not add bullets for small documentation changes or internal refactorings.
  • Each bullet should briefly describe the change to the end user.
  • If the change is related to a function, put the name of the function early in the bullet.
  • If the bullet is related to a GitHub issue or pull request, reference it by number in parentheses before the final period: (#123)..
  • Order bullets alphabetically by function name. Put all bullets that don't mention function names at the beginning.

來源與署名

來源:posit-dev/skills位於r-lib/r-package-development提交e20b71b

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架