简体   繁体   中英

Extract function argument description from R package

I know there are ways to extract all arguments to a function using, for example, rlang::fn_fmls . But is it possible to extract the description of one of these arguments from the package documentation?

For example, what if I wanted to extract the description of the na.rm argument to base::sum() ?

I'm imagining something like:

get_argument_description(fn = 'base::sum', arg = 'na.rm')

Return:

"logical. Should missing values (including NaN) be removed?"

You could try to read the associated help file for that function, and grep the line where \item{argument} . However, multi-line help texts are allowed, if the next line does not start with a \ you would want to grab that too.

This answer shows a way to acess the file, then it is just a matter of grabbing the correct line(s). I also want to highlight a different function in tools ,

tools:::.Rd_get_text()

Which almost gets you where you want, (if you find the correct line)

library(tools)
db = Rd_db("base")
tools:::.Rd_get_text(x = db[["sum.Rd"]])[21] # 21 is the line you want here

[1] "   na.rm: logical.  Should missing values (including 'NaN') be removed?"

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