Specialised lapply functions for R6 or other OOP classes. This is simply a wrapper that detects if FUN is a function, in which case lapply is used as usual, or a string, in which case the given field/method is returned as a list.

loapply(X, FUN, ...)

Arguments

X, ...

See lapply

FUN

Either a function to apply to each element of X, as in lapply or the field/method name of an OOP object (see examples)

Examples

## lapply as usual
loapply(c(1, 2, 3), identity)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 3
#> 

## For R6 objects
objs <- list(ooplah$new(), ooplah$new())
# Public field
loapply(objs, "oop")
#> [[1]]
#> [1] "oop"
#> 
#> [[2]]
#> [1] "oop"
#> 
# Public method
loapply(objs, "hello")
#> [[1]]
#> function () 
#> "Hello World, Ooplah!"
#> <environment: 0x7f859bb51ab8>
#> 
#> [[2]]
#> function () 
#> "Hello World, Ooplah!"
#> <environment: 0x7f859bb74298>
#>