--- title: "Get started with pkginspectr" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Get started with pkginspectr} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` pkginspectr answers simple questions about R packages: how many files, tests and help pages does a package have, and how many of its lines are code, comments or documentation? It works on local folders, GitHub repositories and CRAN packages, returns tidy tibbles, and draws bar charts right in the console. ```{r setup} library(pkginspectr) ``` ## A package to play with pkginspectr ships a tiny example package so you can try everything offline. `pkginspectr_example()` returns its path: ```{r} example <- pkginspectr_example() ``` Every function below takes a `path`. It can be: * a local folder, such as `"."` for the package you are working on, * a GitHub repository, such as `"r-lib/cli"`, or * the name of a CRAN package, such as `"desc"`. Remote packages can be pinned to a version, and prefixes make the source explicit: | `path` | Meaning | |---------------------------------|-------------------------------------------| | `"r-lib/cli@v3.6.0"` | GitHub tag, branch or commit | | `"github::apache/arrow/r"` | A package in a subfolder of a repository | | `"cran::desc@1.4.0"` | An older CRAN release | | `"local::pkgs/mypkg"` | A local folder, never looked up online | GitHub and CRAN packages are downloaded once and cached for the rest of the R session. With `cache = "persistent"`, fixed versions (CRAN releases, GitHub tags and commits) are kept across sessions too. `pkg_cache_clear()` empties the cache. ## Files and folders: `pkg_structure()` ```{r} pkg_structure(example) ``` `num_r_files` counts the scripts in `R/`, split into files that define at least one function and files that do not. `num_test_files` counts R files under `tests/`. ## Lines of code: `pkg_lines()` ```{r} lines <- pkg_lines(example) lines ``` The columns break `total_lines` down in two ways, and each adds up to the total: * **by type**: `code_lines`, `comment_lines`, `roxygen_lines` and `blank_lines`; * **by location**: `r_lines` (in `R/`), `test_lines` (in `tests/`) and `other_lines` (e.g. `data-raw/`). ```{r} with(lines, code_lines + comment_lines + roxygen_lines + blank_lines) with(lines, r_lines + test_lines + other_lines) ``` `function_lines` counts the code lines inside function definitions, so it is part of `code_lines`. Lines are classified with R's own parser rather than pattern matching. A `#` inside a string is not a comment, a `{` inside a string does not open a block, and functions are found however they are written: with `<-`, with `=`, as `\(x)` lambdas, or as anonymous functions passed to other calls. ## Everything at once: `pkg_summary()` `pkg_summary()` combines both tables. For GitHub or CRAN packages it downloads each package only once, so it is the quickest way to get everything: ```{r} pkg_summary(example) ``` ## Individual functions: `pkg_functions()` `pkg_functions()` goes one level deeper and returns one row per named function, with its size, number of arguments and whether the package exports it: ```{r} fns <- pkg_functions(example) fns ``` That makes questions such as "which functions are longest?" or "how much of the package is internal?" one-liners: ```{r} fns[order(fns$code_lines, decreasing = TRUE), c("name", "code_lines")] table(exported = fns$exported) ``` ## Following a package over time `pkg_versions()` lists every CRAN release of a package with its date, and `pkg_history()` runs `pkg_summary()` on several of them. By default it picks five releases spread across the package's history, always including the first and the latest: ```{r, eval = FALSE} pkg_versions("dplyr") history <- pkg_history("dplyr") history |> dplyr::select(package, version, date, num_r_files, code_lines) ``` Plot it with `type = "metric"` to see each metric grow release by release. Rows are labelled with their version: ```{r, eval = FALSE} pkg_plot(history, type = "metric") ``` Use `versions` to pick releases yourself, and `cache = "persistent"` to keep them for next time, since old releases never change: ```{r, eval = FALSE} pkg_history("dplyr", versions = c("0.8.0", "1.0.0", "1.1.0"), cache = "persistent") ``` ## Derived metrics pkginspectr returns raw counts on purpose: they are unambiguous and add up. Ratios are often more useful, especially when comparing packages of very different sizes, and each is a single line of dplyr. Copy the ones you need. ```{r, message = FALSE} library(dplyr, warn.conflicts = FALSE) summary <- pkg_summary(example, quiet = TRUE) ``` ### Package-level ratios ```{r} summary |> transmute( package, # Lines in tests/ for every line in R/ test_to_code = test_lines / r_lines, # Share of all lines that are comments or roxygen documentation comment_density = (comment_lines + roxygen_lines) / total_lines, # Share of code that sits inside function definitions function_share = function_lines / code_lines, # Average size of a script in R/ lines_per_r_file = r_lines / num_r_files ) ``` `r_lines` includes the roxygen comments in `R/`, so `test_to_code` is a conservative measure: heavily documented packages score a little lower. ### Function-level summaries `pkg_functions()` lends itself to per-package summaries of function size and API surface: ```{r} functions <- pkg_functions(example, quiet = TRUE) |> filter(location == "R") |> group_by(package) |> summarise( n_functions = n(), n_exported = sum(exported, na.rm = TRUE), share_internal = mean(!exported, na.rm = TRUE), median_lines = median(code_lines), longest = max(code_lines), .groups = "drop" ) functions ``` ### Combining tables Joining the two gives ratios that need both, such as help pages per exported function. `num_docs` counts every `.Rd` file, including package-level and internal topics, so treat it as a rough indicator: ```{r} summary |> select(package, num_docs) |> left_join(functions, by = "package") |> transmute(package, num_docs, n_exported, docs_per_export = num_docs / n_exported) ``` ### Plotting derived metrics `pkg_plot()` works on any data frame with a `package` column, so derived metrics can be plotted too. Use `type = "metric"`, which scales each metric on its own, because ratios on different scales do not belong on one bar chart: ```{r, eval = FALSE} pkg_summary(c("r-lib/cli", "r-lib/fs", "desc"), quiet = TRUE) |> transmute( package, test_to_code = test_lines / r_lines, comment_density = (comment_lines + roxygen_lines) / total_lines ) |> pkg_plot(type = "metric", title = "Testing and documentation") ``` ## What gets counted pkginspectr counts the files that `R CMD build` would put into the package tarball. It skips version-control folders such as `.git/`, R's standard exclusions such as `.Rproj.user/` and `.Rhistory`, and anything listed in the package's `.Rbuildignore`, which often includes `docs/` and `data-raw/`. This matters when comparing packages: a local checkout and the CRAN release of the same package give comparable numbers. To count `.Rbuildignore`d files as well, use `include_ignored = TRUE`: ```{r, eval = FALSE} pkg_lines(".", include_ignored = TRUE) ``` ## Plotting in the console `pkg_plot()`, or simply `plot()`, draws bar charts in the console. The default view shows one panel per package, with each metric as a share of its total: ```{r} plot(lines) ``` With several packages, `type = "metric"` shows one panel per metric instead, with bars scaled to the largest value: ```{r, eval = FALSE} pkg_summary(c("r-lib/cli", "r-lib/fs", "desc")) |> pkg_plot(type = "metric") ``` Bars are plain characters, so you can restyle them: ```{r} pkg_plot( lines, bar_col = "darkorange", glyph = "=", start = "[", end = "]", max_bars = 20, title = "examplepkg at a glance" ) ``` ## Quiet mode All functions report progress as they go. Use `quiet = TRUE` to turn the messages off, for example in scripts or reports: ```{r} pkg_lines(example, quiet = TRUE) ```