papply.Rd
Works like lapply(), but for PEcAn Settings and MultiSettings objects
papply(settings, fn, ..., stop.on.error = FALSE)
A MultiSettings
, Settings
,
or list
to operate on
The function to apply to settings
additional arguments to fn
Whether to halt execution if a single element in
settings
results in error. See Details.
A single fn
return value, or a list of such values
(coerced to MultiSettings
if appropriate; see 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 fn
is a Settings
object,
then papply
will coerce the returned list into a new
MultiSettings
.
If settings
is a Settings
object,
then papply
knows to call fn
on it directly.
If settings
is a generic list
,
then papply
coerces it to a Settings
object
and then calls fn
on it directly.
This is meant for backwards compatibility with old-fashioned PEcAn
settings lists, but could have unintended consequences
By default, papply
will proceed even if fn
throws an
error for one or more of the elements in settings
.
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.
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: 0x55d623746ab8>
#> <environment: namespace:base>
#>
#> $d
#> [1] "D"
#>
#> attr(,"class")
#> [1] "Settings" "SafeList" "list"
#>
#> attr(,"class")
#> [1] "MultiSettings" "list"