Giovanni Colitti
Giovanni Colitti

Reputation: 2344

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?"

Upvotes: 1

Views: 176

Answers (1)

mhovd
mhovd

Reputation: 4067

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?"

Upvotes: 2

Related Questions