setDefaults(name, ...)
unsetDefaults(name, confirm = TRUE)
getDefaults(name = NULL, arg = NULL)
importDefaults(calling.fun)
formals
, but only returning values set by setDefaults
for the name
function. Calling getDefaults()
(without
arguments) returns in a character vector of all functions currently
having defaults set (by setDefaults
).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 to getOption(name_of_function.Default)
.
NULL
user-
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 by importDefaults
take lower precedence
than arguments specified by the user in the function call.
options
that 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.
Assigning NULL
to any argument will remove the argument from
the defaults list.
importDefaults
should 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 to
setDefaults
, 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 containing importDefaults
)
will override the values set in the global default environment.
options
my.fun <- function(x=3)
{
importDefaults('my.fun')
x^2
}
my.fun() # returns 9
setDefaults(my.fun, x=10)
my.fun() # returns 100
my.fun(x=4) # returns 16
getDefaults(my.fun)
formals(my.fun)
unsetDefaults(my.fun, confirm=FALSE)
getDefaults(my.fun)
my.fun() # returns 9
Run the code above in your browser using DataLab