simple_rapply: Alternative for rapply

Description Usage Arguments Details Value See Also Examples

View source: R/Misc.R

Description

Function to be used as an alternative for rapply (recursive version of lapply)
It differs in two important aspects:

  1. It handles NULL as a value, instead of seeing it as an empty list. See examples for comparison

  2. If you set inclLists to TRUE, it also applies fn over the lists itself

Usage

1
simple_rapply(x, fn, ..., classes = "ANY", inclLists = "No")

Arguments

x

Any object, but usually a list (if not a list, the result is identical to calling fn(x, ...))

fn

A function to apply over the elements of x

...

Additional arguments passed on to fn

classes

A character vector of class names, or "ANY" to match any class. Other classes are returned unmodified

inclLists

Should fn be also applied to the lists themselves? see 'Examples'. Can be FALSE, 'No', 'First', 'Last' or TRUE. First and Last apply fn to the list themselves either before or after applying to the elements themselves. TRUE and FALSE are for backward compatibility, and are casted to 'No' and 'First', with a warning

Details

Further note that this implementation is comparable to running rapply with how='replace'

Value

a list with the same structure as x, with fn applied over the elements

See Also

rapply

Examples

1
2
3
4
5
6
7
8
9
L <- list(l=list(m=4, o=NULL, n=3), p=list(NULL), q=c(t=6, r=4,s=5), k=1)
rapply(L, is.null, how='replace')
any(unlist(rapply(L, is.null, how='replace')))
simple_rapply(L, is.null)
any(unlist(rapply(L, is.null)))

rapply(L, function(x) {if(!is.null(names(x))) x[order(names(x))] else x}, how='replace')
simple_rapply(L, function(x) {if(!is.null(names(x))) x[order(names(x))] else x})
simple_rapply(L, function(x) {if(!is.null(names(x))) x[order(names(x))] else x}, inclLists=TRUE)

EmilBode/EmilMisc documentation built on Feb. 24, 2020, 4:11 p.m.