Package 'kwb.default'

Title: Get and Set Function Argument Defaults
Description: Functions to get and set default values for the formal arguments of user defined functions.
Authors: Hauke Sonnenberg [aut, cre] , Michael Rustler [ctb] , FAKIN [fnd], Kompetenzzentrum Wasser Berlin gGmbH [cph]
Maintainer: Hauke Sonnenberg <[email protected]>
License: MIT + file LICENSE
Version: 0.1.0
Built: 2024-10-27 05:50:05 UTC
Source: https://github.com/KWB-R/kwb.default

Help Index


Get the default value that is defined for a function's argument

Description

Get the default value that is defined for a function's argument

Usage

getDefault(funName, argName, default = NULL, warn = TRUE)

Arguments

funName

Name of the function

argName

Name of the formal argument

default

Default value to use if no default value is stored for argument argName of function funName

warn

if TRUE (default) a message is given if no defaults are defined for the given function or argument and if default is returned instead.

Value

default value that is defined for the formal argument argName of the user-defined function funName

See Also

setDefault

Examples

# Once you have defined a function...
hello <- function(firstName = getDefault("hello", "firstName")) {
  cat("Hello", firstName, "\n")
}

# ... you can define the default value for its arguments...
setDefault("hello", firstName = "Peter")

# ... and read it back with getDefault()...
getDefault("hello", "firstName")

Define the default value for a function's argument

Description

Define the default value for a function's argument

Usage

setDefault(funName, ...)

Arguments

funName

Name of the function

...

default value assignments in the form of key = value pairs

See Also

getDefault

Examples

## Not run: 

# This will lead to an error if funtion "hello" is not defined
setDefault("hello", firstName = "Peter")

# Define the function and use getDefault instead of a constant default value
hello <- function(
  firstName = getDefault("hello", "firstName"),
  lastName = getDefault("hello", "lastName")
)
{
  cat(paste0("Hello ", firstName, " ", lastName, "!\n") )
}

# Now you can define the argument defaults
setDefault("hello", firstName = "Don", lastName = "Quichote")

# If you call the function without arguments, the defaults are used
hello()

# You can now change the defaults without changing the function definition
setDefault("hello", firstName = "Mona", lastName = "Lisa")

hello()

## End(Not run)