| name | r-documentation-patterns |
| description | R documentation patterns with roxygen2, pkgdown, vignettes, examples, and package site structure. |
R Documentation Patterns
Overview
Best practices and patterns for documenting R code, packages, and projects. Covers README files, code comments, function documentation, and project-level documentation.
README Documentation
README Structure
# Package Name
<!-- badges: start -->
[](https://github.com/user/pkg/actions/workflows/R-CMD-check.yaml)
[](https://CRAN.R-project.org/package=pkg)
[](https://lifecycle.r-lib.org/articles/stages.html#stable)
[](https://app.codecov.io/gh/user/pkg?branch=main)
<!-- badges: end -->
## Overview
Brief description of what the package does (2-3 sentences).
## Installation
```r
# From CRAN
install.packages("pkg")
# Development version from GitHub
# install.packages("pak")
pak::pak("user/pkg")
Usage
library(pkg)
result <- main_function(data)
Getting Help
License
MIT © Author Name
### Badges
```r
# Common badges
usethis::use_badge("CRAN status")
usethis::use_lifecycle_badge("stable")
usethis::use_github_actions_badge("R-CMD-check")
usethis::use_coverage("codecov")
Code Comments
Comment Guidelines
normalized_counts <- counts / library_sizes * 1e6
normalized_counts <- counts / library_sizes * 1e6
Section Headers
data <- read_csv("input.csv")
validate_data(data)
clean_data <- data |>
remove_missing() |>
transform_variables()
model <- fit_model(clean_data)
TODO Comments
Function Documentation Style
Simple Function
calculate_bmi <- function(weight, height) {
weight / height^2
}
Complex Function
cv_survival <- function(formula, data, folds = 10, stratify = TRUE, seed = NULL) {
}
Package Documentation
DESCRIPTION File
Package: pkgname
Title: What the Package Does (One Line, Title Case)
Version: 0.1.0
Authors@R:
person("First", "Last", , "email@example.com", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
More details about the package can go here across
multiple lines.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
URL: https://github.com/user/pkgname
BugReports: https://github.com/user/pkgname/issues
Depends:
R (>= 4.1)
Imports:
dplyr (>= 1.0.0),
ggplot2
Suggests:
testthat (>= 3.0.0),
knitr,
rmarkdown
VignetteBuilder: knitr
Config/testthat/edition: 3
Package-Level Documentation
"_PACKAGE"
Vignette Structure
Basic Vignette
---
title: "Getting Started with pkgname"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Getting Started with pkgname}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
library(pkgname)
Introduction
Brief introduction to the package and its purpose.
Installation
install.packages("pkgname")
Basic Usage
Walk through a simple example.
# Example code
result <- main_function(example_data)
Advanced Features
Cover more complex use cases.
See Also
## Architecture Documentation
### Mermaid Diagrams
```markdown
## Package Architecture
```mermaid
flowchart TD
A[User Input] --> B[Input Validation]
B --> C{Valid?}
C -->|Yes| D[Data Processing]
C -->|No| E[Error Message]
D --> F[Model Fitting]
F --> G[Results]
Component Descriptions
## Component Overview
### Data Layer
- `read_data()` - Import various data formats
- `validate_data()` - Check data integrity
- `transform_data()` - Prepare for analysis
### Analysis Layer
- `fit_model()` - Core model fitting
- `cross_validate()` - Performance assessment
- `tune_params()` - Hyperparameter optimization
### Output Layer
- `summarize_results()` - Generate summaries
- `export_results()` - Save to files
- `visualize_results()` - Create plots
Dataset Documentation
"patients"
Documentation Workflow
devtools::document()
devtools::check()
?function_name
pkgdown::build_site()
Best Practices
- Be concise: Documentation should be clear and to the point
- Use examples: Include runnable examples for all exported functions
- Cross-reference: Link to related functions and vignettes
- Keep updated: Update docs when code changes
- Use markdown: Enable markdown in roxygen2 for formatting
- Document edge cases: Explain what happens with unusual inputs
- Include return values: Always document what the function returns