R Code Patterns for Packages

Guide R package code with namespace management and safe state handling.

Updated Feb 15, 2026
One-click install
npx skills add https://github.com/choxos/RPkgAgent --skill r-code-patterns-for-packages
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: R Code Patterns for Packages
Source: https://github.com/choxos/RPkgAgent/tree/main/plugins/r-package-development/skills/r-code-patterns
Command: npx skills add https://github.com/choxos/RPkgAgent --skill r-code-patterns-for-packages

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill addresses common pitfalls and best practices in R package development, ensuring code is robust, maintainable, and adheres to R's ecosystem standards.

Core Features & Use Cases

  • Namespace Management: Guides on using pkg::fun() and @importFrom to manage dependencies correctly.
  • Code Restrictions: Details forbidden functions (library(), source(), :::) and explains why they should be avoided in package code.
  • State Management: Provides patterns for handling global state, options, and environments safely.
  • Build vs. Load Time: Clarifies what code executes during package build versus when a user loads the package.
  • Use Case: A developer is unsure how to properly import functions from other R packages into their own package. This skill provides clear examples and explanations on using explicit namespace calls and @importFrom directives.

Quick Start

Follow the guidelines in this skill to correctly manage R package code and avoid common errors.

Frequently Asked Questions about R Code Patterns for Packages

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why should I avoid using library() and source() in R package code?

Using library() and source() in R package code breaks namespace management and creates unpredictable state. Instead, use pkg::fun() or @importFrom directives to declare dependencies, ensuring robust and maintainable package behavior.

How do I properly import functions from other R packages into my own package?

To properly import functions for R package development, use explicit namespace calls like pkg::fun() or add @importFrom directives in your Roxygen tags. This ensures dependencies are declared correctly without loading entire packages.

What is the difference between build-time and load-time execution in R packages?

Build-time execution in R packages runs code when the package is compiled, while load-time execution runs when a user loads the package. Understanding this distinction is critical for safe global state manipulation and avoiding unexpected side effects.

When should I use requireNamespace() instead of library() in R package development?

Use requireNamespace() in R package development for conditional dependency checks, allowing graceful fallback if a package is missing. Unlike library(), it returns a boolean and avoids altering the search path, maintaining namespace integrity.

Why does using the triple colon ::: cause issues in R package code?

The triple colon ::: accesses internal package objects and is forbidden in R package code because it bypasses namespace management, creating fragile dependencies on unexported API elements. Stick to exported functions via pkg::fun() or @importFrom.

Can I include non-ASCII characters in R package scripts?

Non-ASCII characters in R scripts are forbidden in R package development because they cause encoding issues during package building and testing. R package code best practices require sticking to ASCII characters to ensure cross-platform compatibility.