retrieve: An R alternative to the lodash 'get' in JavaScript

View source: R/retrieve_and_has.R

retrieveR Documentation

An R alternative to the lodash get in JavaScript

Description

This is a handy function for retrieving items deep in a nested structure without causing error if not found

Usage

retrieve(sourceList, path, default = NA)

Arguments

sourceList

The list()/c() that is to be searched for the element

path

A string that can be separated by [,] or ., the string "elementname1.1.elementname" is equivalent to "elementname1[[1]]]elementname". Note that the function doesn't check the validity of the path - it only separates and tries to address that element with '[[]]'.

default

The value to return if the element isn't found

Value

Returns a sub-element from sourceList or the default value.

See Also

Other lodash similar functions: has()

Examples

source <- list(a = list(b = 1, `odd.name` = 'I hate . in names', c(1,2,3)))
retrieve(source, "a.b")
retrieve(source, "a.b.1")
retrieve(source, "a.odd\\.name")
retrieve(source, "a.not_in_list")


Gmisc documentation built on Aug. 26, 2023, 1:07 a.m.