How to find all functions in an R package?
Categories:
Discovering All Functions Within an R Package

Learn various methods to list and inspect all functions exported or internal to an R package, enhancing your understanding and debugging capabilities.
When working with R packages, it's often necessary to explore their contents, especially to find available functions, understand their arguments, or debug issues. R provides several powerful tools and functions to help you systematically discover and inspect functions within any installed package. This article will guide you through the most common and effective methods.
Listing Exported Functions
The most common scenario is to find functions that are explicitly made available for users by the package developer. These are known as 'exported' functions. R's ls()
function, combined with package:
, is the primary way to achieve this. Additionally, the getNamespaceExports()
function offers a more direct approach.
# Load a package (e.g., 'dplyr')
library(dplyr)
# Method 1: Using ls() with package namespace
ls("package:dplyr")
# Method 2: Using getNamespaceExports()
getNamespaceExports("dplyr")
Listing exported functions from the 'dplyr' package.
ls("package:your_package")
method only works after the package has been loaded using library()
or require()
. getNamespaceExports()
works even if the package is not explicitly loaded, as long as it's installed.Inspecting All Functions (Exported and Internal)
Sometimes, you might need to delve deeper and find not just the exported functions, but also the internal, unexported functions used by the package. These are typically helper functions not intended for direct user interaction but can be useful for advanced debugging or understanding package internals. Accessing these requires using the triple colon operator :::
or inspecting the package's namespace directly.
# Method 1: Using ::: for a specific unexported function (if known)
dplyr:::`%||%`
# Method 2: Listing all objects in the package's namespace
# This includes both exported and unexported functions/objects
ls(getNamespace("dplyr"))
# To filter specifically for functions:
all_objects <- ls(getNamespace("dplyr"))
all_functions <- all_objects[sapply(paste0("dplyr:::", all_objects), function(x) {
tryCatch(is.function(eval(parse(text=x))), error = function(e) FALSE)
})]
head(all_functions)
Discovering both exported and internal functions within 'dplyr'.
flowchart TD A[Start] --> B{Load Package?} B -- Yes --> C[Use `ls("package:pkg")`] B -- No --> D[Is Package Installed?] D -- Yes --> E[Use `getNamespaceExports("pkg")`] D -- No --> F[Install Package] F --> E C --> G{Need Internal Functions?} E --> G G -- Yes --> H[Use `ls(getNamespace("pkg"))`] G -- No --> I[End] H --> J[Filter for functions] J --> I
Decision flow for finding R package functions.
Exploring Function Signatures and Documentation
Once you've identified a function, the next step is often to understand how to use it. R provides easy ways to view a function's arguments and access its documentation. The args()
function shows the arguments, while ?
or help()
provides comprehensive documentation for exported functions.
# Example: Inspecting the 'filter' function from dplyr
# View function arguments
args(dplyr::filter)
# Access documentation for an exported function
?dplyr::filter
# For unexported functions, documentation is usually not available directly.
# You might need to inspect the source code.
# Example: View source code of an unexported function (if available)
# print(dplyr:::`%||%`) # Uncomment to see source
Inspecting function arguments and accessing documentation.
:::
) is generally discouraged for production code, as their behavior and existence are not guaranteed across package versions. Use them primarily for debugging or deep understanding.