formal_call: Run a function only using its formal arguments.

Description Usage Arguments Details Author(s) Examples

View source: R/formalCall.R

Description

Extract from the function definition its formal arguments. Then from ..., only use the arguments that are in the formal definition and drop the rest. This helps avoid conflicts when you are passing arguments with ... but is not used by all of the functions you need.

Usage

1

Arguments

fun

A function to use (the actual function, not the character)

...

A set of arguments. Only those matching the formal definition of fun will be used when evaluating fun.

Details

Some functions are not defined with the ... argument, and they will choke if you use them in your functions and pass arguments they are not using.

Author(s)

L. Collado-Torres

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
## Lets define a very simple identity function
ident <- function(x) return(x)

## Now lets use it inside another function
maxIdent <- function(x, ...) {
    m <- max(x, ...)
    y <- ident(m, ...)
    return(y)
}
## Seems to work
maxIdent(1:10)

## But not when we actually use '...'
## Not run: 
maxIdent(1:10, 11)

## End(Not run)

## Lets now use formal_call() to run ident() only using the arguments it
## has as part of its function definition.
maxIdent2 <- function(x, ...) {
    m <- max(x, ...)
    y <- formal_call('ident', x = m, ...)
    return(y)
}

## Works now =)
maxIdent2(1:10, 11)

lcolladotor/dots documentation built on Aug. 24, 2019, 5:23 p.m.