| Title: | Tidy Tools for Inspecting and Comparing R Package Structure |
|---|---|
| Description: | Inspect the structure and source code of R packages from local folders, 'GitHub' repositories or 'CRAN'. Summarise file counts, folder structure and lines of code (blank, comment, 'roxygen', function and test lines) as tidy tibbles. Lines are classified with R's own parser, and files excluded by '.Rbuildignore' are skipped, so local folders and released packages can be compared on equal terms. Compare several packages side by side and visualise the results as Unicode bar charts in the console. |
| Authors: | Daniel Dauber [aut, cre] |
| Maintainer: | Daniel Dauber <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.0.9000 |
| Built: | 2026-12-02 01:04:03 UTC |
| Source: | https://github.com/ddauber/pkginspectr |
Deletes the cache directory used by pkginspectr to store downloaded
CRAN and GitHub packages. This is useful for freeing up disk space or
forcing fresh downloads in future analyses.
pkg_cache_clear(type = c("session", "persistent"))pkg_cache_clear(type = c("session", "persistent"))
type |
A string indicating which cache to clear. Must be one of:
|
You can choose to clear either the session-level cache (temporary files created during the current R session) or the persistent cache (files stored across sessions in a user-specific cache directory).
Used for side effects. Invisibly returns the path that was cleared,
or NULL if no cache was found.
# Clear the cache of the current R session pkg_cache_clear("session") # Clear packages cached across R sessions pkg_cache_clear("persistent")# Clear the cache of the current R session pkg_cache_clear("session") # Clear packages cached across R sessions pkg_cache_clear("persistent")
Return one row per named function defined in the R source files of one or more packages, with its size, number of arguments and whether it is exported. Use it to find the largest functions, or to see how much of a package is internal.
pkg_functions( path = ".", include_ignored = FALSE, quiet = FALSE, cache = c("session", "persistent", "none") )pkg_functions( path = ".", include_ignored = FALSE, quiet = FALSE, cache = c("session", "persistent", "none") )
path |
A character vector of packages. Each element is one of:
Without a prefix, existing local folders take precedence, then
With several packages, one that cannot be analysed (a typo, a failed
download) does not stop the others: it is reported in a warning and
returned as a row of |
include_ignored |
If |
quiet |
If |
cache |
Where to keep downloaded GitHub and CRAN packages:
Use |
A function is listed when it is assigned to a name at the top level of a
file, with <-, = or <<-, written as function(...) or \(...).
Functions defined inside other functions, inside lists or R6 classes, or
with setMethod() are not listed. Files that fail to parse are skipped
with a warning.
The same files are analysed as in pkg_lines(): R source files in R/,
tests/ and data-raw/ that R CMD build would ship.
A tibble of class pkginspectr_functions with one row per
function and columns:
package, version
file: path relative to the package root, e.g. "R/utils.R".
location: "R", "tests" or "other" (e.g. data-raw/).
name: the function's name.
line: the line where the definition starts.
code_lines: code lines in the definition, excluding comments and
blank lines (as in pkg_lines()).
n_args: number of arguments, counting ... as one.
exported: whether the package's NAMESPACE exports the function,
via export() or exportPattern(). NA for functions outside R/
or when the package has no readable NAMESPACE. S3 methods registered
with S3method() are not exported in this sense.
Rows are ordered by package, file and line. A package without functions contributes no rows.
pkg_lines() for package-level line counts.
fns <- pkg_functions(pkginspectr_example()) fns # The largest functions fns[order(fns$code_lines, decreasing = TRUE), c("name", "code_lines")] # Exported vs internal functions table(fns$exported) # The ten longest functions in cli pkg_functions("r-lib/cli", quiet = TRUE) |> dplyr::slice_max(code_lines, n = 10)fns <- pkg_functions(pkginspectr_example()) fns # The largest functions fns[order(fns$code_lines, decreasing = TRUE), c("name", "code_lines")] # Exported vs internal functions table(fns$exported) # The ten longest functions in cli pkg_functions("r-lib/cli", quiet = TRUE) |> dplyr::slice_max(code_lines, n = 10)
Run pkg_summary() on several CRAN releases of one package to see how it
grew over time. By default n releases are picked evenly across the
package's history, always including the first and the latest; or name the
versions you want.
pkg_history( package, versions = NULL, n = 5, include_ignored = FALSE, quiet = FALSE, cache = c("session", "persistent", "none") )pkg_history( package, versions = NULL, n = 5, include_ignored = FALSE, quiet = FALSE, cache = c("session", "persistent", "none") )
package |
Name of a CRAN package, such as |
versions |
Optional character vector of versions to analyse, e.g.
|
n |
Number of releases to pick when |
include_ignored |
If |
quiet |
If |
cache |
Where to keep downloaded GitHub and CRAN packages:
Use |
Each release is downloaded from CRAN. Old releases never change, so
cache = "persistent" is a good choice if you will revisit them.
A pkginspectr_tbl like pkg_summary(), with one row per release,
sorted by version number (oldest first), and an extra date column after version. Plot it with
pkg_plot(type = "metric") to compare releases side by side.
pkg_versions() to list the releases.
# Five releases spread across desc's history history <- pkg_history("desc") history pkg_plot(history, type = "metric") # Specific releases, kept across R sessions pkg_history("desc", versions = c("1.2.0", "1.4.0"), cache = "persistent")# Five releases spread across desc's history history <- pkg_history("desc") history pkg_plot(history, type = "metric") # Specific releases, kept across R sessions pkg_history("desc", versions = c("1.2.0", "1.4.0"), cache = "persistent")
Analyse the R source files (.R and .r) in R/, tests/ and data-raw/
of one or more packages and count their lines, broken down two ways: by
type (code, comment, roxygen, blank) and by location (R/, tests/,
elsewhere).
pkg_lines( path = ".", include_ignored = FALSE, quiet = FALSE, cache = c("session", "persistent", "none") )pkg_lines( path = ".", include_ignored = FALSE, quiet = FALSE, cache = c("session", "persistent", "none") )
path |
A character vector of packages. Each element is one of:
Without a prefix, existing local folders take precedence, then
With several packages, one that cannot be analysed (a typo, a failed
download) does not stop the others: it is reported in a warning and
returned as a row of |
include_ignored |
If |
quiet |
If |
cache |
Where to keep downloaded GitHub and CRAN packages:
Use |
Lines are classified with R's own parser (utils::getParseData()), so
strings that contain # or unbalanced braces are counted correctly, and
functions are detected however they are written (<-, =, \(x)
lambdas, or anonymous functions inside calls). Files that fail to parse
fall back to a line-based heuristic and their function lines are not
counted; a warning names each such file.
Only files that R CMD build would ship are analysed, so results for a
local folder are comparable with results for a CRAN tarball. In particular,
anything listed in .Rbuildignore (often data-raw/) is skipped unless
include_ignored = TRUE.
A pkginspectr_tbl with one row per package and columns:
package, version
total_lines: all lines in the analysed files.
By type (these four add up to total_lines):
code_lines: lines containing code, including code followed by a
comment and lines inside multi-line strings.
comment_lines: lines containing only a regular (#) comment.
roxygen_lines: lines containing only a roxygen (#') comment.
blank_lines: empty or whitespace-only lines.
By location (these three add up to total_lines):
r_lines: lines in R/.
test_lines: lines in tests/.
other_lines: lines elsewhere (e.g. data-raw/).
And:
function_lines: code lines inside function definitions (a subset of
code_lines). Nested functions are counted once.
pkg_structure() for file counts, pkg_summary() for both, and
pkg_plot() to visualise the result.
pkg_lines(pkginspectr_example()) # GitHub repositories and CRAN packages are downloaded and cached pkg_lines(c("r-lib/cli", "desc")) # Specific versions, kept across sessions pkg_lines(c("r-lib/[email protected]", "cran::[email protected]"), cache = "persistent")pkg_lines(pkginspectr_example()) # GitHub repositories and CRAN packages are downloaded and cached pkg_lines(c("r-lib/cli", "desc")) # Specific versions, kept across sessions pkg_lines(c("r-lib/[email protected]", "cran::[email protected]"), cache = "persistent")
Draw Unicode bar charts of results from pkg_structure(), pkg_lines()
or pkg_summary() directly in the console. Calling plot() on any of
these results does the same.
pkg_plot( x, type = c("package", "metric"), text_col = "#4670D4", bar_col = "#4670D4", bg_col = NULL, glyph = "█", start = "▏", end = "▕", max_bars = NULL, title = NULL ) ## S3 method for class 'pkginspectr_tbl' plot(x, ...)pkg_plot( x, type = c("package", "metric"), text_col = "#4670D4", bar_col = "#4670D4", bg_col = NULL, glyph = "█", start = "▏", end = "▕", max_bars = NULL, title = NULL ) ## S3 method for class 'pkginspectr_tbl' plot(x, ...)
x |
A result of |
type |
|
text_col, bar_col
|
Colours for text and bars: a hex code such as
|
bg_col |
Optional background colour for the whole chart. |
glyph |
Character used to fill the bars. Must be a single-width character; wide characters such as emoji are replaced by a heavy horizontal line. |
start, end
|
Characters drawn at the start and end of each bar. Use
|
max_bars |
Maximum bar width in characters. Defaults to 40 for
|
title |
Title shown above the chart. |
... |
Arguments passed on to |
There are two views:
type = "package" draws one panel per package. Bars show each metric's
share of the package total: num_files for file counts and
total_lines for line counts. num_dirs counts folders rather than
files, so it is shown without a bar.
type = "metric" draws one panel per metric, comparing packages. Bars
are scaled to the largest value of each metric, so the biggest package
always gets a full bar.
x, invisibly.
lines <- pkg_lines(pkginspectr_example()) # One panel per package pkg_plot(lines) # The same, via plot() plot(lines) # One panel per metric, for comparing packages pkg_plot(lines, type = "metric") # Restyle the bars pkg_plot(lines, bar_col = "darkorange", glyph = "=", start = "[", end = "]")lines <- pkg_lines(pkginspectr_example()) # One panel per package pkg_plot(lines) # The same, via plot() plot(lines) # One panel per metric, for comparing packages pkg_plot(lines, type = "metric") # Restyle the bars pkg_plot(lines, bar_col = "darkorange", glyph = "=", start = "[", end = "]")
Examine the file structure of one or more packages and count files, folders, R scripts, tests, vignettes, help pages and data files.
pkg_structure( path = ".", include_ignored = FALSE, quiet = FALSE, cache = c("session", "persistent", "none") )pkg_structure( path = ".", include_ignored = FALSE, quiet = FALSE, cache = c("session", "persistent", "none") )
path |
A character vector of packages. Each element is one of:
Without a prefix, existing local folders take precedence, then
With several packages, one that cannot be analysed (a typo, a failed
download) does not stop the others: it is reported in a warning and
returned as a row of |
include_ignored |
If |
quiet |
If |
cache |
Where to keep downloaded GitHub and CRAN packages:
Use |
Only files that R CMD build would ship are counted: version-control
folders (e.g. .git/), R's built-in build exclusions (e.g. .Rproj.user/,
.Rhistory) and, unless include_ignored = TRUE, anything listed in
.Rbuildignore (e.g. docs/, data-raw/). This makes results for a local
folder comparable with results for a CRAN tarball.
A pkginspectr_tbl with one row per package and columns:
package, version
num_files, num_dirs: files and folders in the package.
num_r_files: R source files (.R or .r) in R/.
num_function_r_files, num_non_function_r_files: files in R/ that
do / do not define at least one function (detected by the R parser).
These two add up to num_r_files.
num_test_files: R files under tests/.
num_vignettes: .Rmd, .qmd or .Rnw files under vignettes/.
num_docs: .Rd help files under man/.
num_data_files: data files under data/ (.rda, .RData, .rds,
.csv, .tsv, .tab, .txt, optionally compressed).
pkg_lines() for line counts, pkg_summary() for both, and
pkg_plot() to visualise the result.
pkg_structure(pkginspectr_example()) # GitHub repositories and CRAN packages are downloaded and cached pkg_structure(c("r-lib/cli", "desc"))pkg_structure(pkginspectr_example()) # GitHub repositories and CRAN packages are downloaded and cached pkg_structure(c("r-lib/cli", "desc"))
Combine pkg_structure() and pkg_lines() into one table. Each package is
resolved (and, for GitHub or CRAN, downloaded) only once, so this is the
quickest way to get everything about a set of packages.
pkg_summary( path = ".", include_ignored = FALSE, quiet = FALSE, cache = c("session", "persistent", "none") )pkg_summary( path = ".", include_ignored = FALSE, quiet = FALSE, cache = c("session", "persistent", "none") )
path |
A character vector of packages. Each element is one of:
Without a prefix, existing local folders take precedence, then
With several packages, one that cannot be analysed (a typo, a failed
download) does not stop the others: it is reported in a warning and
returned as a row of |
include_ignored |
If |
quiet |
If |
cache |
Where to keep downloaded GitHub and CRAN packages:
Use |
A pkginspectr_tbl with one row per package: package, version,
then the columns of pkg_structure() followed by those of pkg_lines().
pkg_plot() to visualise the result.
pkg_summary(pkginspectr_example()) pkg_summary(c("r-lib/cli", "desc")) |> pkg_plot(type = "metric")pkg_summary(pkginspectr_example()) pkg_summary(c("r-lib/cli", "desc")) |> pkg_plot(type = "metric")
Return every version of a package published on CRAN, with its release
date, sorted by version number (oldest first). Use it to choose versions for pkg_history() or for
"cran::pkg@version" specifications.
pkg_versions(package)pkg_versions(package)
package |
Name of a CRAN package, such as |
Dates are those recorded by CRAN: the file date in CRAN's archive for
older releases, and the Date/Publication field for the current one.
A date is NA if CRAN does not report it.
A tibble with one row per release and columns package,
version, date (a Date) and current (TRUE for the release
currently on CRAN). Archived packages have no current release.
pkg_history() to analyse several releases.
pkg_versions("desc")pkg_versions("desc")
pkginspectr ships a tiny package, examplepkg, with known contents. It lets
examples run offline and makes results easy to check by hand: its R files
use <-, = and \(x) function definitions, a lower-case .r test file,
and strings containing # and unbalanced braces.
pkginspectr_example()pkginspectr_example()
Path to the root directory of the example package.
pkginspectr_example() pkg_structure(pkginspectr_example())pkginspectr_example() pkg_structure(pkginspectr_example())