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
#> # A tibble: 127 x 18
#> pkg_name pkg_src fn_name exported r_file r_file_path ln_start ln_end
#> <chr> <chr> <chr> <lgl> <chr> <chr> <int> <int>
#> 1 pkgattrs GitHub are_gi… FALSE utils… ./R/utils.R 4 12
#> 2 pkgattrs GitHub are_lo… FALSE utils… ./R/utils.R 17 24
#> 3 pkgattrs GitHub from_g… TRUE from_… ./R/from_g… 8 16
#> 4 pkgattrs GitHub functi… TRUE funct… ./R/functi… 11 21
#> 5 pkgattrs GitHub functi… TRUE funct… ./R/functi… 35 54
#> 6 pkgattrs GitHub get_fc… FALSE get_f… ./R/get_fc… 13 139
#> 7 pkgattrs GitHub pkgatt… TRUE pkgat… ./R/pkgatt… 13 363
#> 8 pkgattrs GitHub produc… FALSE funct… ./R/functi… 57 103
#> 9 pkgattrs GitHub write_… TRUE write… ./R/write_… 10 101
#> 10 blastula GitHub `%||%` FALSE utils… ./R/utils.R 41 43
#> # … with 117 more rows, and 10 more variables: fn_lines <int>, code <dbl>,
#> # comment <dbl>, blank <dbl>, roxygen <dbl>, total_lines <dbl>,
#> # pkg_repo <chr>, pkg_path <chr>, n_pkg_fns_called <int>,
#> # pkg_fns_called <list>
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.