| 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] (ORCID: <https://orcid.org/0000-0001-9134-2871>), Michael Rustler [ctb] (ORCID: <https://orcid.org/0000-0003-0647-7726>), FAKIN [fnd], Kompetenzzentrum Wasser Berlin gGmbH [cph] |
| Maintainer: | Hauke Sonnenberg <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-05-12 07:04:11 UTC |
| Source: | https://github.com/KWB-R/kwb.default |
Get the default value that is defined for a function's argument
getDefault(funName, argName, default = NULL, warn = TRUE)getDefault(funName, argName, default = NULL, warn = TRUE)
funName |
Name of the function |
argName |
Name of the formal argument |
default |
Default value to use if no default value is stored for
argument |
warn |
if |
default value that is defined for the formal argument argName
of the user-defined function funName
# 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")# 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
setDefault(funName, ...)setDefault(funName, ...)
funName |
Name of the function |
... |
default value assignments in the form of |
## 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)## 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)