如何在CRAN或本地R包中搜索函数使用实例(含正则匹配)
Great question! Hunting down real-world usage examples of R functions—even the unexported ones that don’t show up in documentation—is such a smart way to learn their idiomatic use. Let’s break down how to do this for local packages, CRAN packages, and how to leverage regex for similar functions:
Method 1: Use devtools (Quick & Convenient)
The devtools package has a handy find_callers() function that lets you search for function calls directly, including unexported ones. Here’s how to use it:
library(devtools) # Search for calls to the `filter` function in dplyr (including internal uses) find_callers("filter", package = "dplyr") # Search for multiple functions at once, e.g., filter and select find_callers(c("filter", "select"), package = "dplyr")
To target unexported functions, just use their full qualified name (with three colons):
# Find calls to dplyr's internal filter_rows function find_callers("dplyr:::filter_rows", package = "dplyr")
Method 2: Manual File Traversal (For Custom Regex)
If you need full control with regex, you can directly scan a package’s source files. This is perfect for matching groups of similar functions:
# Get the path to your target package pkg_path <- system.file(package = "dplyr") # Grab all R script files in the package r_files <- list.files(file.path(pkg_path, "R"), pattern = "\\.R$", full.names = TRUE) # Search for lines containing calls to filter OR select (regex OR operator) matches <- lapply(r_files, function(file) { lines <- readLines(file) grep("filter|select", lines, value = TRUE) }) # Clean up results by removing empty entries Filter(length, matches)
Want to match all functions starting with tbl_? Adjust the regex to grep("tbl_\\w+", lines, value = TRUE)—the \\w+ matches any word characters after the prefix.
To find how functions are used across the entire CRAN ecosystem, you’ll first need to download the package source code, then apply the same local search methods:
library(pkgdepends) # Download the source code of your target package to a temporary directory pkg_download <- pkg_download("dplyr", destdir = tempdir()) # Get the path to the downloaded source src_path <- pkg_download$path[1] # Now scan the R files just like we did for local packages r_files <- list.files(file.path(src_path, "R"), pattern = "\\.R$", full.names = TRUE) matches <- lapply(r_files, function(file) { lines <- readLines(file) grep("arrange", lines, value = TRUE) }) Filter(length, matches)
If you want to search multiple CRAN packages, you can loop through a list of package names (just be aware this can be time-consuming for large numbers of packages!).
- Target actual function calls: To avoid matching the word in comments or strings, use a regex that targets function syntax:
grep("\\bfilter\\(", lines, value = TRUE). The\\bensures it’s a standalone word, and\\(matches the opening parenthesis of a function call. - Match unexported function patterns: Use
grep("dplyr:::\\w+", lines, value = TRUE)to find all calls to unexported functions in dplyr. - Fuzzy match similar functions: For functions with shared prefixes (like
mutate,mutate_at,mutate_if), usegrep("mutate\\w*\\(", lines, value = TRUE)to catch them all.
- Filter out comments/strings: To exclude lines that are comments, add a check to skip lines starting with
#:grep("^[^#].*filter\\(", lines, value = TRUE). - Performance considerations: Large packages (like
tidyverseordata.table) can take a few seconds to scan. Batch-searching dozens of CRAN packages will take longer, so narrow your focus to relevant packages. - Unexported function caveat: Unexported functions aren’t part of a package’s public API—they can change or disappear in future versions, so use their usage as a learning tool rather than relying on them in your own code.
内容的提问来源于stack exchange,提问作者andrewH

