extract_metadata: Extract docstring and meta data from an anonymous function

Description Usage Arguments Value Examples

View source: R/metadata.R

Description

Currently the docstring must come first and the metadata list must come second. Both must precede any other code in the function. These constraints may be relaxed in the future.

Usage

1
extract_metadata(expr, env = parent.frame(), skip_name = TRUE)

Arguments

expr

an expression with an optional docstring as the first statement

env

Environment in which to search for functions

skip_name

Do not attempt to find function matching expressions of type 'name'. If FALSE, then a function will by searched for with name expr if expr is a name. This leads to expr being evaluated, which raises errors outside the purview of Rmonad. For example, extract_metadata(stop("dying")).

Value

a list of three elements, the expression with the docstring and metadata removed, the docstring itself, and the metadata.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
## extract metadata from a block 
expr <- substitute(
  {
    "this is the docstring"
    list(foo="this is meta data")
    5 * 32
  }
)
extract_metadata(expr)

foo <- function(x,y){
  "docstring"
  list(meta="data")
  x + y
}
## extract metadata from a function name
extract_metadata(substitute(foo), skip_name=FALSE)

## extract from a partially applied function
extract_metadata(substitute(foo(y=2)))

arendsee/rmonad documentation built on Oct. 10, 2018, 6:26 p.m.