knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
library(pkgattrs) library(tidyverse)
The pkgattrs package is useful for getting information on the contents of any R package. One of the things that can be done is generating a summary of functions available in one or more packages. We can conveniently do this using the pkgattrs()
function. Here is an example where we can create an informative table of the functions in the pkgattrs
and blastula
packages (hosted on GitHub).
fn_info <- pkgattrs( from_github("rich-iannone/pkgattrs"), from_github("rich-iannone/blastula") )
The resulting tibble contains the following information in each record:
pkg_name
)pkg_src
)fn_name
)exported
)r_file
)r_file
(r_file_path
) r_file
where the function starts (ln_start
) and ends (ln_end
)fn_lines
)code
), for comments (comment
), and for roxygen statements (roxygen
), and, the fn_lines
lines that are blank (blank
): the sum of all these is given in total_lines
cyclocomp
)pkg_repo
)pkg_path
)fn_name
(n_pkg_fns_called
)fn_name
(pkg_fns_called
)fn_info
The package also supplies functions for visualizing the relationships between a package's functions as a network graph. For example, we could obtain a function information tibble for the stationary package, transform that to a graph, and then examine this network with the function_graph_all()
function.
pkgattrs(from_github("rich-iannone/stationary")) %>% function_graph_all()
In this graph, the green nodes show the functions that are exported, and, the relative sizing of nodes is scaled the number of package functions called by each. Each edge represents the relationship called_in
.
We can also focus on a subgraph with a single function. The function function_graph_single()
can be used with the function graph object, taking a function name to show all the package functions that the function calls. In the following example, we can examine which functions are called by the print.ptblank_agent()
method.
pkgattrs(from_github("rich-iannone/pointblank")) %>% function_graph_single(target_fn = "print.ptblank_agent")
Finally, the package has a means to write out a given package's API with the write_pkg_api()
function. For the pkgattrs package (this package), we can generate a file that lists the exported functions along with each of the function arguments and default values.
write_pkg_api(filename = "pkg_api")
This example generates the following text in the API
file:
from_github(repo) function_graph_all(pkgattrs_tbl, pkg_name = NULL) function_graph_single(pkgattrs_tbl, target_fn, pkg_name = NULL) pkg_api(...) pkgattrs(..., .make_clean = TRUE, .get_cyclocomp = FALSE) write_pkg_api(..., filename = "pkg_api")
You can install pkgattrs from GitHub with:
devtools::install_github("rich-iannone/pkgattrs")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.