| Title: | Static Detection and Citation of R Package and Function Usage |
|---|---|
| Description: | Scans R source files for package and function use, then builds citations from configurable package universes. Supports .R, .Rmd, and .qmd files, resolves unqualified calls by attachment order and re-export origin, and leaves each package collection to define its own citations. See 'stanflow' <https://github.com/VisruthSK/stanflow> for an example usage. |
| Authors: | Visruth Srimath Kandali [aut, cre, cph] (ORCID: <https://orcid.org/0009-0005-9097-0688>) |
| Maintainer: | Visruth Srimath Kandali <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.0 |
| Built: | 2026-08-23 11:05:39 UTC |
| Source: | https://github.com/VisruthSK/ascribe |
Given a named list mapping package names to character vectors of
function names (as produced by collect_pkg_funs()), creates an
inverted index mapping function names to character vectors of
packages that export them.
build_export_index(exports)build_export_index(exports)
exports |
Named list. Names are package names, values are character vectors of function names. |
Named list mapping function names to character vectors of package names.
exports <- list( pkgA = c("foo", "bar"), pkgB = c("foo", "baz") ) build_export_index(exports)exports <- list( pkgA = c("foo", "bar"), pkgB = c("foo", "baz") ) build_export_index(exports)
Given a named list mapping package names to character vectors of
function names, creates a named character vector mapping
"pkg::fun" keys to the origin package. Functions whose origin
cannot be determined fall back to the providing package.
build_origin_map(exports)build_origin_map(exports)
exports |
Named list. Names are package names, values are character vectors of function names. |
Environment mapping "pkg::fun" keys to origin package names.
exports <- list( stats = collect_pkg_funs("stats"), utils = collect_pkg_funs("utils") ) build_origin_map(exports)exports <- list( stats = collect_pkg_funs("stats"), utils = collect_pkg_funs("utils") ) build_origin_map(exports)
Given a character vector of package names, computes the export
lists, inverted export index, origin map, and version snapshot
needed by scan_usage(). All packages must be installed.
build_universe_data(packages)build_universe_data(packages)
packages |
Character vector of package names. |
An ascribe_universe object with components:
The input package names.
Named list mapping package names to character
vectors of exported function names (from collect_pkg_funs()).
Named list mapping function names to
character vectors of packages (from build_export_index()).
Environment mapping "pkg::fun" keys
to origin packages (from build_origin_map()).
Named list mapping package names to version strings.
build_universe_data(c("stats", "utils"))build_universe_data(c("stats", "utils"))
Builds citations from scan_usage() results. Package collections supply
their own citation records and package-citation policy.
cite_usage( usage, package_citations = new.env(parent = emptyenv(), hash = TRUE), function_citations = new.env(parent = emptyenv(), hash = TRUE), package_citation = utils::citation, always_cite = character(), format = c("bibtex", "bibentry") )cite_usage( usage, package_citations = new.env(parent = emptyenv(), hash = TRUE), function_citations = new.env(parent = emptyenv(), hash = TRUE), package_citation = utils::citation, always_cite = character(), format = c("bibtex", "bibentry") )
usage |
Results returned by |
package_citations |
An environment of package citation entries. Missing
packages use |
function_citations |
An environment of function citation entries,
keyed by |
package_citation |
A function that accepts a package name and returns
its citation entries. Defaults to |
always_cite |
Character vector of packages to cite in addition to the packages found by the scan. |
format |
One of |
A BibTeX character vector or a bibentry object.
path <- tempfile(fileext = ".R") writeLines("cli::cli_alert_info('hi'); fastmatch::fmatch(1, 1:5)", path) universe <- build_universe_data(c("cli", "fastmatch")) usage <- scan_usage(path, universe) cite_usage(usage) unlink(path)path <- tempfile(fileext = ".R") writeLines("cli::cli_alert_info('hi'); fastmatch::fmatch(1, 1:5)", path) universe <- build_universe_data(c("cli", "fastmatch")) usage <- scan_usage(path, universe) cite_usage(usage) unlink(path)
Returns a character vector of function names exported by pkg,
including methods of exported and namespace-internal R6 classes.
collect_pkg_funs(pkg)collect_pkg_funs(pkg)
pkg |
Package name (character scalar). |
Character vector of function/method names.
collect_pkg_funs("stats")collect_pkg_funs("stats")
Computes scanner data for the given packages and saves it to
sysdata.rda with variable names prefixed by prefix. This is
intended for use in a downstream package's data-raw/sysdata.R
script.
generate_universe_sysdata( packages, prefix, extra_vars = list(), include_scanner_defaults = FALSE, file )generate_universe_sysdata( packages, prefix, extra_vars = list(), include_scanner_defaults = FALSE, file )
packages |
Character vector of package names. |
prefix |
Character scalar used to name the saved objects
(e.g., |
extra_vars |
Named list of additional objects to include in the saved file (e.g., citation environments). |
include_scanner_defaults |
If |
file |
Output path (required). |
The generated variables are:
.{prefix}_pkgsCharacter vector of package names.
.{prefix}_exportsNamed list of exported functions per package.
.{prefix}_export_indexInverted index: function name to packages.
.{prefix}_origin_mapEnvironment: "pkg::fun" keys to origin.
.{prefix}_pkg_versionsNamed list of version strings.
When include_scanner_defaults is TRUE, .stdlib_funs and
.scan_skip_dirs are also saved.
Invisibly returns the result of build_universe_data().
file <- tempfile(fileext = ".rda") generate_universe_sysdata(c("stats", "utils"), "my", file = file) unlink(file)file <- tempfile(fileext = ".rda") generate_universe_sysdata(c("stats", "utils"), "my", file = file) unlink(file)
Given a package and function name, determines which package the function actually originates from (handling re-exports).
resolve_origin(pkg, name)resolve_origin(pkg, name)
pkg |
Package name (character scalar). |
name |
Function name (character scalar). |
The origin package name, or NA_character_ if undetermined.
resolve_origin("stats", "median")resolve_origin("stats", "median")
Statically scans R source files for package attachments and function calls.
It recognizes library(), require(), requireNamespace(), and use().
scan_usage( path = ".", universe, ignore_unqualified_functions = .stdlib_funs, strict = FALSE, skip_dirs = .scan_skip_dirs, metapackages = NULL, use_knitr = FALSE )scan_usage( path = ".", universe, ignore_unqualified_functions = .stdlib_funs, strict = FALSE, skip_dirs = .scan_skip_dirs, metapackages = NULL, use_knitr = FALSE )
path |
A single project directory (searched recursively) or a vector of files (.R/.Rmd/.qmd). |
universe |
A universe returned by |
ignore_unqualified_functions |
Defaults to exports from base R packages
listed in |
strict |
If |
skip_dirs |
Character vector of directory names to skip when scanning a
directory. Defaults to |
metapackages |
Named list mapping attached package names to additional
packages that should be treated as co-attached for unqualified resolution.
Defaults to |
use_knitr |
Logical. If |
Explicit package references from library(), require(),
requireNamespace(), use(), and pkg::fun are only recorded when their
package is included in allowed_packages. The scanner attributes an
unqualified function only when library() or require() attached a package
earlier in the same file and the supplied indexes can resolve the call.
metapackages can add packages to that attachment set. If several attached
packages export the function, the most recently attached match wins. The
scanner attributes known re-exports to their origin package and otherwise to
the resolved package.
A list of packages, resolved functions, and ambiguous function calls.
path <- tempfile(fileext = ".R") writeLines( c( "# one messy analysis file", "library(stats)", "requireNamespace(\"utils\")", "filter(1:10, rep(1, 3))", "utils::head(letters)" ), path ) universe <- build_universe_data(c("stats", "utils")) scan_usage(path, universe, ignore_unqualified_functions = character()) unlink(path)path <- tempfile(fileext = ".R") writeLines( c( "# one messy analysis file", "library(stats)", "requireNamespace(\"utils\")", "filter(1:10, rep(1, 3))", "utils::head(letters)" ), path ) universe <- build_universe_data(c("stats", "utils")) scan_usage(path, universe, ignore_unqualified_functions = character()) unlink(path)