简体   繁体   中英

Access list of functions and metadata for github dev R package

Please Note: This is cross-posted from here where it hasn't received a response. So I'm adding it here.

I'm currently co-developing an R package on github which can be installed using devtools::install_github('repo/pkgname) , as usual.

We have diligently used roxygen2 to document the individual functions. We have split the functions into "internal" ( @keywords internal ) vs. "external" ( @export ) so that the user gets to use the external functions ie pkgname::external_<fn_name> and access documentation. They can also use ::: to access the internal functions if they wish.

For some meta analysis of our package it would be nice to have a functionality that produced a tidy tibble with the following columns:

  • function name,
  • function type ie internal/external (accessible by :: or ::: to the user)
  • More metadata eg another column containing parameter names for each function ie @param values
  • documentation strings for each parameter

As a crude version (non-tibble format) for say dplyr. One can do something like:

library(dplyr) # Assume installed already
ls('package:dplyr')

This produces a character vector of function names, but not a tidy tibble with more useful metadata.

Ideally we would be able to produce this tibble after doing devtools::load_all(".") in our package development, to track changes in real-time.

Are there any existing R packages that can help generate such a metadata tibble ? Or can such a function be developed for this using existing R packages?

Would appreciate any help associated with this.

I have an answer to my question, which may help others. It turns out this metadata can be accessed using the amazing pkgdown package.

See below for code to use when you have opened an RStudio project attached to a package you are developing (using devtools ):

# Setup - install required libraries
# install.packages(c("pkgdown", "here"))

# If you are in your local package directory, run the following
# to get the required package metadata
pkg <- pkgdown::as_pkgdown(pkg = here::here())

# Inspect the topics object, which contains function metadata
pkg$topics %>% dplyr::glimpse()

# Get list of all functions and just required metadata
pkg_fns_all <- pkg$topics %>%
    dplyr::select(name, file_in, internal)

# Get the non-internal functions, acccessed using pkgname::function
pkg_fns_user <- pkg_fns_all %>% dplyr::filter(!internal)

# Get the internal functions, acccessed using pkgname:::function
pkg_fns_internal <- pkg_fns_all %>% dplyr::filter(internal)

Hope this helps others:slight_smile:

A few small outstanding items:

  • I'm not sure how to get access to individual function @param values from the above, but if anyone can add some details around that it would be useful.
  • I'm not sure how to apply this to CRAN installed packages on my system eg dplyr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM