The searchable package provides flexibile methods for searching and subsetting objects by matching names using case (in)sensitivity, regular expressions, fixed expressions or other user defined patterns. This is accomplished by overloading the standard [ operator to allow the string[r|i] style match modifiers. These can be applied to the target list|vector|objects. When no match modifiers are used, the default is standard R behavior. So this is a drop-in replacement.

searchable was designed to be extensible. Developers can use searchable to build thier own flexible, high performance dictionary and thesaurus data structures or their own match modifiers

( NOTE: All examples in this vignette use the magrittr package. The pipe operator adds some clarity. )

Quickstart

Making an object searchable only requires passing that object to searchable.

mylist <- list(a=1, b=2, c=3, B=2) %>% searchable()

That's it mylist now uses the searchable behaviors. By default, searchable does not change search behavior. mylist continues to be searched using R's exact matching behavior. This allows searchable objects to be used as drop-in replacements for plain-old R objects:

> mylist['b']
$b
[1] 3

This is what is normally expect from R. Not very useful, but ...

Changing Default Search Behavior (per object)

The searchable function can be used to define a default search behavior by passing a modifier function as the second argument:

mylist <- list(a=1, b=2, c=3, B=2) %>% searchable( regex )

Ad hoc searches

searchable allows the search behavior to be specified on-the-fly using match-modifiers. To perform ad hoc searches, the match-modifiers are applied to the search term. Here is an example of a case-insensitive subset:

> mylist[ ignore.case('b') ]
$b
[1] 3

$B
[1] 2

Here is an example of a regular-expression subset:

> mylist[ regex('[ab]') ]
$a
[1] 1

$b
[1] 3

NOTE: Ad hoc searches take priority over a default search. If both are supplied, the Ad hoc search takes priority.

It gets tedious to add match-modifiers to

Searching can now be performed using stringr's match modifiers.

> mylist[ ignore.case('b') ]
$b
[1] 1

$B
[1] 2

Custom Match Modifiers



decisionpatterns/searchable documentation built on Jan. 8, 2019, 3:28 a.m.