Learn R Programming

searchable (version 0.1.2)

searchable-class: searchable

Description

Creates a searchable class that allows for modification for searches.

is.searchable

Usage

searchable(object, ...)

## S3 method for class 'searchable': show(object)

is.searchable(object)

Arguments

object
searchable object or object to be made searchable
...
one or more stringr-style match modifying sunctions, See ?match.modifiers for details.

Value

  • By default, the extraction from a searchable objects is not a class searchable. It is assumed that in most cases, the developer will not want another searchable object.

reverse.lookup

When performing a reverse lookup, values (not names) are searched. The corresponding names are returned. NOTE: this is highly experimental and only works for atomic vectors. It is uncertain how this might be applied to recursive structures.

replacement

searchable can be used to replace objects as well. See ?extract for additional exemples.

multiple dimension objects

Multiple dimension ojects such as data.frames, data.tables, matrices and arrays are not supported at this time.

Adding new modifiers

It is possible to add additional search modifiers ... (vignette)

Details

The searchable class allows for non-standard, stringr-like searches when extracting (or replacing) objects. The following modification are (at present) supported: fixed, ignore.case, perl and reverse.lookup.

Search modification can be applied either to the object being searched or the search pattern(s). To apply them to the searched object add the modifiers to the modifiers slot of the object. These are applied after any user supplied modifiers.

searchable is designed to be minimally invase. No modification to the object or its names are made when classing an object as searcable and if no search modifiers are enabled, the object behaves as a regular, "non-searchable" object.

See Also

extract ignore.case perl reverse.lookup

Examples

Run this code
# ATOMIC VECTORS:
    v <- c( a=1, b=2, B=3, c=4, c2=5 )
    sv <- searchable(v)

  # EXTRACT:
    sv$a

    sv[['a']]
    sv[[ ignore.case('A') ]]

    sv[ ignore.case('b') ]
    sv[ perl('c') ]
    sv[ fixed('c') ]


  # REPLACEMENT:
    sv$a               <- "first"
    sv[['a']]          <- "1st"
    sv[[ perl('c.') ]] <- "third"

    sv[ perl('c.?') ]   <- "3rd"


  # MODIFIERS TO SEARCH TARGET/OBJECT
    sv <- searchable(v, ignore.case )
    sv$A
    sv['b']
    sv['B']


  # RECURSIVE LISTS:
    l <- list( a=1, b=2, c=3 )
    sl <- searchable(l)
    sl[["b"]]
    sl[[ ignore.case("B") ]]

  # USE WITH MAGRITTR
   sl[[ "B"  %>% ignore.case ]]
    "b" %>% sl[[.]]
    "B" %>% ignore.case %>% sl[[ . ]]

Run the code above in your browser using DataLab