简体   繁体   中英

Extract documentation from an r package

I'm trying to write an R function which will extract exported functions from an R package and return a list containing for each function, the call signature, and the description from documentation.

Getting the list of exported functions can be done with relative ease by doing the following:

# Extracts all exported function names from dplyr
library(dplyr) 
lsf.str("package:dplyr")

I can also get the call signature for a given function using the name:

# Extract call signature for the join function in dplyr
lsf.str("package:dplyr")[10] %>%
  get() %>%
  deparse() %>%
  head(1)

I can even automatically open up the help section for each one of those functions:

# Opens help for the join function in dplyr
help(lsf.str("package:dplyr")[10], package = "dplyr")

But how can can I return a string containing the text of the description for this function? So, for the join function in dplyr, it ought to return:

These are generic functions that dispatch to individual tbl methods - see the method documentation for details of individual data sources. x and y should usually be from the same data source, but if copy is TRUE, y will automatically be copied to the same source as x.

Any ideas?

library(tidyverse)

lsf.str("package:dplyr")[10] %>% 
  help("dplyr") %>%
  utils:::.getHelpFile() %>% 
  keep(~attr(.x, "Rd_tag") == "\\description") %>% 
  map(as.character) %>% 
  flatten_chr() %>% 
  paste0(., collapse="")
## [1] "\nThese are generic functions that dispatch to individual tbl methods - see the\nmethod documentation for details of individual data sources. x and\ny should usually be from the same data source, but if copy is\nTRUE, y will automatically be copied to the same source as x.\n"

Removing newlines is an exercise left to the OP :-)

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