Learn R Programming

crmPack (version 2.0.0)

h_rapply: Recursively Apply a Function to a List

Description

[Experimental]

This helper function recursively iterates through a "list-like" object and it checks whether an element is of a given class. If it so, then it replaces that element by the result of an execution of a given function. Otherwise, and if the element is of length greater than 1 (i.e. not a scalar), it replaces that element by the result of h_rapply(), recursively called for that element. In the remaining case, that is, the element is not of a given class and is a scalar, then that element remains unchanged.

Usage

h_rapply(x, fun, classes, ...)

Value

"list-like" object of similar structure as x.

Arguments

x

(any)
"list-like" object for which subsetting operator [[ is defined.

fun

(function)
a function of one "principal" argument, passing further arguments via ....

classes

(character)
class names.

...

further arguments passed to function fun.

Examples

Run this code
# Some model function.
my_model <- function() {
  alpha0 <- mean(1:10)
  alpha1 <- 600000
}

# Replace format of numbers using `formatC` function.
h_rapply(
  x = body(my_model),
  fun = formatC,
  classes = c("integer", "numeric"),
  digits = 3,
  format = "E"
)

Run the code above in your browser using DataLab