replaceDefault: Replace default values in functions and check parameter list

Description Usage Arguments Details Value Author(s) Examples

View source: R/replaceDefault.R

Description

replaceDefault takes a function and a list of values to replace the default values. In addition it checks that the resulting function is fit for use in other functions, e.g. as cost function in optimiseSD.

Usage

1
replaceDefault(fun, newDefaults = list(), type)

Arguments

fun

function

newDefaults

list of new default values: elements must be named, names have to be those of parameters of fun (not necessarily all of them); then default values of fun are replaced; elements without or with other names cause a warning.

type

character string indicating the purpose of the resulting function, e.g. to be used as summaryFun in summaryPlumes; these function calls require certain parameters: it is checked if the fun has them and if all additional parameters have default values (after replacing defaults), and generates warnings, if not. For possible values, see details.

Details

Dependent on the type, a list of required parameters is generated and compared to the parameters of fun: all required parameters must exist and all other parameters need to have default values (in the resulting function, when defaults of newDefaults have been inserted)

"summaryFun.summaryPlumes" (summaryFun in summaryPlumes): "x", "weight"
"fun.simulationsApply" (fun, fun_l, fun_p, fun_pl in simulationsApply): "x", "nout"
"funR.simulationsApply" (fun_Rp or fun_Rl in simulationsApply): "x", "nout", "weight"
"funRR.simulationsApply" (fun_Rpl in simulationsApply): "x", "nout", "weight_l", "weight_p"
"fun_interpolation.interpolate" (fun_interpolation in interpolate): "data", "newdata", "y"
"fun.spatialSpread" (fun in spatialSpread): "allLocations", "locations"
"fun_R.spatialSpread" (fun_R in spatialSpread): "x"
"costFun" (costFun in optimiseSD): "simulations", "locations"
"optimisationFun.optimiseSD" (optimisationFun in optimiseSD): "simulations", "costFun", "locationsAll", "locationsFix", "locationsInitial", "aimcost", "aimNumber", "nameSave"
"evalFunc.rbga.bin"(evalFunc in rbga.bin): "chromosome"

For "fun.simulationsApply" and "funR.simulationsApply" the parameter "nout" must exist and have default value.

replaceDefault checks, if fun is a function. It does not check if the new defaults have the same class as the old ones.

Value

A list

fun

function fun with replaced default values

accept

logical if the function passed all tests (i.e. no warnings) – mainly needed when replaceDefault is called inside of other functions (e.g. simulationsApply)

Author(s)

Kristina B. Helle, kristina.helle@uni-muenster.de

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# examples with warnings
fun1 = "a"
fun_1 = replaceDefault(fun1)
fun2 = function(x){sum(x)}
fun_2 = replaceDefault(fun2, type = "fun.simulationsApply")
fun3 = function(simulations, locations){1}
# change default of 'simulations' 
fun_3 = replaceDefault(fun3, type = "costFun", 
  newDefaults = list(nout = 1, simulations = 10))  
# example without warnings 
# (new default values have different class, not tested)
fun6 = function(x = 17, weight = "a", extraWeight = matrix(1:12, nrow = 3)){}
fun_6 = replaceDefault(fun6, 
  newDefaults = list(weight = 3, extraWeight = "c"), 
  type =  "summaryFun.summaryPlumes") 

sensors4plumes documentation built on May 1, 2019, 10:27 p.m.