Works like lapply(), but for PEcAn Settings and MultiSettings objects

papply(settings, fn, ..., stop.on.error = FALSE)

Arguments

settings

A MultiSettings, Settings, or list to operate on

fn

The function to apply to settings

stop.on.error

Whether to halt execution if a single element in settings results in error. See Details.

...

additional arguments to fn

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 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.

Examples

# NOT RUN {
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")
print(f(multiSettings, d="D"), TRUE)

# }