Manage Default Argument Values for quantmod Functions
Defaults.RdUse globally specified defaults, if set, in place of formally specified default argument values. Allows user to specify function defaults different than formally supplied values, e.g. to change poorly performing defaults, or satisfy a different preference.
Usage
setDefaults(name, ...)
unsetDefaults(name, confirm = TRUE)
getDefaults(name = NULL, arg = NULL)
importDefaults(calling.fun)Details
- setDefaults
Provides a wrapper to R
optionsthat allows the user to specify any name=value pair for a function's formal arguments. Only formal name=value pairs specified will be updated.Values do not have to be respecified in subsequent calls to
setDefaults, so it is possible to add new defaults for each function one at a time, without having to retype all previous values. AssigningNULLto any argument will remove the argument from the defaults list.namecan be an unquoted, bare symbol only at the top-level. It must be a quoted character string if you callsetDefaultsinside a function.- unsetDefaults
Removes name=value pairs from the defaults list.
- getDefaults
Provides access to the stored user defaults. Single arguments need not be quoted, multiple arguments must be in a character vector.
- importDefaults
A call to
importDefaultsshould be placed on the first line in the body of the function. It checks the user's environment for globally specified default values for the called function. These defaults can be specified by the user with a call tosetDefaults, and will override any default formal parameters, in effect replacing the original defaults with user supplied values instead. Any user-specified values in the parent function (that is, the function containingimportDefaults) will override the values set in the global default environment.
Value
- setDefaults
None. Used for it's side effect of setting a list of default arguments by function.
- unsetDefaults
None. Used for it's side effect of unsetting default arguments by function.
- getDefaults
A named list of defaults and associated values, similar to
formals, but only returning values set bysetDefaultsfor thenamefunction. CallinggetDefaults()(without arguments) returns in a character vector of all functions currently having defaults set (bysetDefaults).This does not imply that the returned function names are able to accept defaults (via
importDefaults), rather that they have been set to store user defaults. All values can also be viewed with a call togetOption(name_of_function.Default).- importDefaults
None. Used for its side-effect of loading all non-
NULLuser- specified default values into the current function's environment, effectively changing the default values passed in the parent function call. Like formally defined defaults in the function definition, default values set byimportDefaultstake lower precedence than arguments specified by the user in the function call.
Note
- setDefaults
At present it is not possible to specify
NULLas a replacement for a non-NULLdefault, as the process interpretsNULLvalues as being not set, and will simply use the value specified formally in the function. IfNULLis what is desired, it is necessary to include this in the function call itself.Any arguments included in the actual function call will take precedence over
setDefaultsvalues, as well as the standard formal function values. This conforms to the current user experience in R.Like
options, default settings are not kept across sessions. Currently, it is not possible to pass values for ... arguments, only formally specified arguments in the original function definition.- unsetDefaults
unsetDefaultsremoves the all entries from theoptionslists for the specified function. To remove single function default values simply set the name of the argument toNULLinsetDefaults.- importDefaults
When a function implements
importDefaults, non-named arguments may be ignored if a global default has been set (i.e. notNULL). If this is the case, simply name the arguments in the calling function.This should also work for functions retrieving formal parameter values from
options, as it assigns a value to the parameter in a way that looks like it was passed in the function call. So any check onoptionswould presumably disregardimportDefaultsvalues if an argument was passed to the function.
Examples
my.fun <- function(x=3)
{
importDefaults('my.fun')
x^2
}
my.fun() # returns 9
#> [1] 9
setDefaults(my.fun, x=10)
#> Error in setDefaults(my.fun, x = 10): no function named 'my.fun' was found
my.fun() # returns 100
#> [1] 9
my.fun(x=4) # returns 16
#> [1] 16
getDefaults(my.fun)
#> NULL
formals(my.fun)
#> $x
#> [1] 3
#>
unsetDefaults(my.fun, confirm=FALSE)
#> NULL
getDefaults(my.fun)
#> NULL
my.fun() # returns 9
#> [1] 9