Works like lapply(), but for PEcAn Settings and MultiSettings objects
Arguments
- settings
A
MultiSettings,Settings, orlistto operate on- fn
The function to apply to
settings- ...
additional arguments to
fn- stop.on.error
Whether to halt execution if a single element in
settingsresults in error. See Details.
Value
A single fn return value, or a list of such values
(coerced to MultiSettings if appropriate; see Details)
Details
papply is mainly used to call a function on each
Settings object in a MultiSettings object,
and returning the results in a list.
It has some additional features, however:
If the result of
fnis aSettingsobject, thenpapplywill coerce the returned list into a newMultiSettings.If
settingsis aSettingsobject, thenpapplyknows to callfnon it directly.If
settingsis a genericlist, thenpapplycoerces it to aSettingsobject and then callsfnon it directly. This is meant for backwards compatibility with old-fashioned PEcAn settings lists, but could have unintended consequencesBy default,
papplywill proceed even iffnthrows an error for one or more of the elements insettings. Note that if this option is used, the returned results list will have entries for only those elements that did not result in an error.
Examples
f = function(settings, ...) {
# Here's how I envisioned a typical use case within a standard PEcAn function
if(is.MultiSettings(settings)) {
return(papply(settings, f, ...))
}
# Don't worry about the beolow, it's just some guts to make the function do something we can see
l <- list(...)
for(i in seq_along(l)) {
ind <- length(settings) + 1
settings[[ind]] <- l[[i]]
if(!is.null(names(l))) {
names(settings)[ind] <- names(l)[i]
}
}
return(settings)
}
# Example
settings1 <- Settings(list(a="aa", b=1:3, c="NA"))
settings2 <- Settings(list(a="A", b=4:5, c=paste))
multiSettings <- MultiSettings(settings1, settings2)
# The fucntion should add element $d = D to either a Settings, or each entry in a MultiSettings
f(settings1, d="D")
#> $a
#> [1] "aa"
#>
#> $b
#> [1] 1 2 3
#>
#> $c
#> [1] "NA"
#>
#> $d
#> [1] "D"
#>
#> attr(,"class")
#> [1] "Settings" "SafeList" "list"
print(f(multiSettings, d="D"), TRUE)
#> $settings.1
#> $a
#> [1] "aa"
#>
#> $b
#> [1] 1 2 3
#>
#> $c
#> [1] "NA"
#>
#> $d
#> [1] "D"
#>
#> attr(,"class")
#> [1] "Settings" "SafeList" "list"
#>
#> $settings.2
#> $a
#> [1] "A"
#>
#> $b
#> [1] 4 5
#>
#> $c
#> function (..., sep = " ", collapse = NULL, recycle0 = FALSE)
#> .Internal(paste(list(...), sep, collapse, recycle0))
#> <bytecode: 0x56175b386f78>
#> <environment: namespace:base>
#>
#> $d
#> [1] "D"
#>
#> attr(,"class")
#> [1] "Settings" "SafeList" "list"
#>
#> attr(,"class")
#> [1] "MultiSettings" "list"