call_flexibly: Construct a flexible function call

View source: R/call-flexibly.R

call_flexiblyR Documentation

Construct a flexible function call

Description

Programming aid: See details for more information on how to use it.

Usage

call_flexibly(
  fn,
  fn.ns,
  fn.ns.sep = "::",
  default = list(),
  verbose = TRUE,
  v.fail = NULL,
  v.skip = NULL
)

Arguments

fn

Character value. Denotes the function to be called.

fn.ns

Character value. Denotes the namespace/package from which to call fn.

default

A named list of arguments that can not be specified by the user.

verbose

Logical. If set to TRUE informative messages regarding the computational progress will be printed.

(Warning messages will always be printed.)

v.fail

The return value in case the function call results in an error.

v.skip

The return value in case of fn is specified as FALSE by the user..

Details

This function takes two strings as input denoting the function to be called and the namespace from which it is to be called. It expects an object of the same name as fn to be in it's calling environment specified with an identically named argument from the user of the function from which call_flexibly() is called.

If that object is a list all named elements of that list are considered to be arguments with which function fn is to be called (= specified list). The names of that list are compared to the names of default_list. Arguments specified in the default list can not be altered and are discarded from the specified list with an informative warning. Subsequently the names of all remaining arguments are compared to the valid arguments of the function to be called and discarded if unused arguments appear in order to prevent the function call from failing. (This does not happen if the function to be called uses the dot-product '...').

If that object is a single TRUE the fn is called with default_list as input.

If that object is anything else the function call is skipped.

Value

The return value of fn.

Examples

# Not run:

 example_fun <- function(plot, runif){

   call_flexibly(fn = "plot",
                 fn.ns = "base",
                 default_list = list(x = 1:10),
                 v.fail = "This failed.",
                 v.skip = "Okey, I skip that."
                 )

   call_flexibly(fn = "runif",
                 fn.ns = "stats",
                 default_list = list(n = 100),
                 v.fail = "This failed.",
                 v.skip = 1:100)

 }


 # call the function

 example_fun(plot = list(y = 1:10, cex = 5, col = "red"), runif = list(max = 100, min = 1))


kueckelj/confuns documentation built on July 4, 2024, 4:53 p.m.