Package 'pkginspectr'

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

Help Index


Clear the cache of downloaded packages

Description

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.

Usage

pkg_cache_clear(type = c("session", "persistent"))

Arguments

type

A string indicating which cache to clear. Must be one of:

  • "session": temporary cache stored in tempdir() (default)

  • "persistent": long-term cache stored in a platform-specific user cache directory via rappdirs::user_cache_dir("pkginspectr")

Details

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).

Value

Used for side effects. Invisibly returns the path that was cleared, or NULL if no cache was found.

Examples

# Clear the cache of the current R session
pkg_cache_clear("session")


# Clear packages cached across R sessions
pkg_cache_clear("persistent")

List the functions defined in packages

Description

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.

Usage

pkg_functions(
  path = ".",
  include_ignored = FALSE,
  quiet = FALSE,
  cache = c("session", "persistent", "none")
)

Arguments

path

A character vector of packages. Each element is one of:

  • A local folder, such as "." or "~/code/mypkg". Use "local::path" to be explicit.

  • A GitHub repository, such as "r-lib/cli" or a github.com URL. Add ⁠@ref⁠ for a branch, tag or commit ("r-lib/[email protected]"). For a package in a subfolder, use the ⁠github::⁠ prefix: "github::owner/repo/subdir".

  • A CRAN package, such as "desc". Add ⁠@version⁠ for an older release ("cran::[email protected]"). Archived packages are supported.

Without a prefix, existing local folders take precedence, then "owner/repo" is treated as GitHub and a bare name as CRAN. A path that does not exist (e.g. "./mypkg") is an error, not a GitHub request.

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 NAs whose package is the specification as given (pkg_functions() leaves it out instead). A single package that fails, or all packages failing, is an error.

include_ignored

If TRUE, also analyse files matched by the package's .Rbuildignore. Version-control folders and R's built-in build exclusions are always skipped.

quiet

If TRUE, suppress progress messages.

cache

Where to keep downloaded GitHub and CRAN packages:

  • "session" (default): a temporary folder, removed when R closes.

  • "persistent": a user cache folder kept across R sessions. Only fixed versions are stored there: CRAN releases and GitHub refs that look like a commit SHA or a version tag ("owner/[email protected]"). Requests that can change over time (no ref, or a branch) always fetch the latest code and use the session cache instead.

  • "none": always download afresh.

Use pkg_cache_clear() to empty a cache.

Details

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.

Value

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.

See Also

pkg_lines() for package-level line counts.

Examples

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)

Follow a package across its CRAN releases

Description

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.

Usage

pkg_history(
  package,
  versions = NULL,
  n = 5,
  include_ignored = FALSE,
  quiet = FALSE,
  cache = c("session", "persistent", "none")
)

Arguments

package

Name of a CRAN package, such as "dplyr".

versions

Optional character vector of versions to analyse, e.g. c("0.8.0", "1.0.0"). See pkg_versions() for what is available. If given, n is ignored.

n

Number of releases to pick when versions is not given. If the package has n releases or fewer, all are used.

include_ignored

If TRUE, also analyse files matched by the package's .Rbuildignore. Version-control folders and R's built-in build exclusions are always skipped.

quiet

If TRUE, suppress progress messages.

cache

Where to keep downloaded GitHub and CRAN packages:

  • "session" (default): a temporary folder, removed when R closes.

  • "persistent": a user cache folder kept across R sessions. Only fixed versions are stored there: CRAN releases and GitHub refs that look like a commit SHA or a version tag ("owner/[email protected]"). Requests that can change over time (no ref, or a branch) always fetch the latest code and use the session cache instead.

  • "none": always download afresh.

Use pkg_cache_clear() to empty a cache.

Details

Each release is downloaded from CRAN. Old releases never change, so cache = "persistent" is a good choice if you will revisit them.

Value

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.

See Also

pkg_versions() to list the releases.

Examples

# 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")

Count lines of R code in packages

Description

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).

Usage

pkg_lines(
  path = ".",
  include_ignored = FALSE,
  quiet = FALSE,
  cache = c("session", "persistent", "none")
)

Arguments

path

A character vector of packages. Each element is one of:

  • A local folder, such as "." or "~/code/mypkg". Use "local::path" to be explicit.

  • A GitHub repository, such as "r-lib/cli" or a github.com URL. Add ⁠@ref⁠ for a branch, tag or commit ("r-lib/[email protected]"). For a package in a subfolder, use the ⁠github::⁠ prefix: "github::owner/repo/subdir".

  • A CRAN package, such as "desc". Add ⁠@version⁠ for an older release ("cran::[email protected]"). Archived packages are supported.

Without a prefix, existing local folders take precedence, then "owner/repo" is treated as GitHub and a bare name as CRAN. A path that does not exist (e.g. "./mypkg") is an error, not a GitHub request.

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 NAs whose package is the specification as given (pkg_functions() leaves it out instead). A single package that fails, or all packages failing, is an error.

include_ignored

If TRUE, also analyse files matched by the package's .Rbuildignore. Version-control folders and R's built-in build exclusions are always skipped.

quiet

If TRUE, suppress progress messages.

cache

Where to keep downloaded GitHub and CRAN packages:

  • "session" (default): a temporary folder, removed when R closes.

  • "persistent": a user cache folder kept across R sessions. Only fixed versions are stored there: CRAN releases and GitHub refs that look like a commit SHA or a version tag ("owner/[email protected]"). Requests that can change over time (no ref, or a branch) always fetch the latest code and use the session cache instead.

  • "none": always download afresh.

Use pkg_cache_clear() to empty a cache.

Details

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.

Value

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.

See Also

pkg_structure() for file counts, pkg_summary() for both, and pkg_plot() to visualise the result.

Examples

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")

Plot package statistics in the console

Description

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.

Usage

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, ...)

Arguments

x

A result of pkg_structure(), pkg_lines() or pkg_summary(). Any data frame with a package column and numeric columns also works.

type

"package" for one panel per package, or "metric" for one panel per metric comparing packages.

text_col, bar_col

Colours for text and bars: a hex code such as "#4670D4" or a colour name. NULL uses the terminal's default colour.

bg_col

Optional background colour for the whole chart. NULL (the default) leaves the terminal background unchanged.

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 "" for no caps.

max_bars

Maximum bar width in characters. Defaults to 40 for type = "package" and 25 for type = "metric". Bars shrink to fit narrow consoles.

title

Title shown above the chart. NULL (the default) chooses one based on the data; "" shows no title.

...

Arguments passed on to pkg_plot().

Details

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.

Value

x, invisibly.

Examples

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 = "]")

Count files and folders in packages

Description

Examine the file structure of one or more packages and count files, folders, R scripts, tests, vignettes, help pages and data files.

Usage

pkg_structure(
  path = ".",
  include_ignored = FALSE,
  quiet = FALSE,
  cache = c("session", "persistent", "none")
)

Arguments

path

A character vector of packages. Each element is one of:

  • A local folder, such as "." or "~/code/mypkg". Use "local::path" to be explicit.

  • A GitHub repository, such as "r-lib/cli" or a github.com URL. Add ⁠@ref⁠ for a branch, tag or commit ("r-lib/[email protected]"). For a package in a subfolder, use the ⁠github::⁠ prefix: "github::owner/repo/subdir".

  • A CRAN package, such as "desc". Add ⁠@version⁠ for an older release ("cran::[email protected]"). Archived packages are supported.

Without a prefix, existing local folders take precedence, then "owner/repo" is treated as GitHub and a bare name as CRAN. A path that does not exist (e.g. "./mypkg") is an error, not a GitHub request.

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 NAs whose package is the specification as given (pkg_functions() leaves it out instead). A single package that fails, or all packages failing, is an error.

include_ignored

If TRUE, also analyse files matched by the package's .Rbuildignore. Version-control folders and R's built-in build exclusions are always skipped.

quiet

If TRUE, suppress progress messages.

cache

Where to keep downloaded GitHub and CRAN packages:

  • "session" (default): a temporary folder, removed when R closes.

  • "persistent": a user cache folder kept across R sessions. Only fixed versions are stored there: CRAN releases and GitHub refs that look like a commit SHA or a version tag ("owner/[email protected]"). Requests that can change over time (no ref, or a branch) always fetch the latest code and use the session cache instead.

  • "none": always download afresh.

Use pkg_cache_clear() to empty a cache.

Details

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.

Value

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).

See Also

pkg_lines() for line counts, pkg_summary() for both, and pkg_plot() to visualise the result.

Examples

pkg_structure(pkginspectr_example())


# GitHub repositories and CRAN packages are downloaded and cached
pkg_structure(c("r-lib/cli", "desc"))

Summarise package structure and lines of code

Description

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.

Usage

pkg_summary(
  path = ".",
  include_ignored = FALSE,
  quiet = FALSE,
  cache = c("session", "persistent", "none")
)

Arguments

path

A character vector of packages. Each element is one of:

  • A local folder, such as "." or "~/code/mypkg". Use "local::path" to be explicit.

  • A GitHub repository, such as "r-lib/cli" or a github.com URL. Add ⁠@ref⁠ for a branch, tag or commit ("r-lib/[email protected]"). For a package in a subfolder, use the ⁠github::⁠ prefix: "github::owner/repo/subdir".

  • A CRAN package, such as "desc". Add ⁠@version⁠ for an older release ("cran::[email protected]"). Archived packages are supported.

Without a prefix, existing local folders take precedence, then "owner/repo" is treated as GitHub and a bare name as CRAN. A path that does not exist (e.g. "./mypkg") is an error, not a GitHub request.

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 NAs whose package is the specification as given (pkg_functions() leaves it out instead). A single package that fails, or all packages failing, is an error.

include_ignored

If TRUE, also analyse files matched by the package's .Rbuildignore. Version-control folders and R's built-in build exclusions are always skipped.

quiet

If TRUE, suppress progress messages.

cache

Where to keep downloaded GitHub and CRAN packages:

  • "session" (default): a temporary folder, removed when R closes.

  • "persistent": a user cache folder kept across R sessions. Only fixed versions are stored there: CRAN releases and GitHub refs that look like a commit SHA or a version tag ("owner/[email protected]"). Requests that can change over time (no ref, or a branch) always fetch the latest code and use the session cache instead.

  • "none": always download afresh.

Use pkg_cache_clear() to empty a cache.

Value

A pkginspectr_tbl with one row per package: package, version, then the columns of pkg_structure() followed by those of pkg_lines().

See Also

pkg_plot() to visualise the result.

Examples

pkg_summary(pkginspectr_example())


pkg_summary(c("r-lib/cli", "desc")) |>
  pkg_plot(type = "metric")

List the CRAN releases of a package

Description

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.

Usage

pkg_versions(package)

Arguments

package

Name of a CRAN package, such as "dplyr". A "cran::" prefix is accepted.

Details

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.

Value

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.

See Also

pkg_history() to analyse several releases.

Examples

pkg_versions("desc")

Path to the example package bundled with pkginspectr

Description

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.

Usage

pkginspectr_example()

Value

Path to the root directory of the example package.

Examples

pkginspectr_example()

pkg_structure(pkginspectr_example())