searchable (version 0.3.3.1)

Searchable-class: Searchable

Description

searchable makes a named object a Searchable target, optionally specifying the default search options.

Usage

searchable(object, type = "std", ...)

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

Arguments

object
searchable object or object to be made searchable
type
character; the type of search to perform
...
additional arguments defining the search pattern. See ?pattern for details.

Value

  • By default, extraction from a searchable objects does not produce a subset that is also searchable. It is assumed that in most cases, the developer will not want another searchable object and only wish to have the subclass.

Diffences from stringr

stringr and stringi are general purpose string manipulations library allowing flexible search and pattern matching against character strings. The searchable package applies this type of matching to objects' names using the standard [ accessor. Thus,

searchable(sv)[ regex('b') ]

returns objects the subset of whose names contain 'b'.

Unlike stringr/i, searchable allows search specification to applied to either the search pattern or search target. When applied to the target, a default search method is configured. All subsequent searches of the searchable target will use this default pattern.

The search method can be specified with the type argument of the searchable function or any of match-modifying functions, e.g. fixed, regex, coll, ignore.case, etc. See examples.

When modifiers are applied to both target and pattern, modifers applied to the pattern take precedence and the target's modifiers are disabled.

Differences from base R

searchable is designed to be minimally invase. When no search types or options are specified, mathcing defaults to R's normal behavior.

Here are the other differnece from standard R operations:

  • $and
  • \[\[
are unaltered by the package. It is unclear, how these operators might accommodate the indeterminate number of matches.

Searches using multiple patterns recylce the patterns, but rather return elements that match any of the patterns.

In base R, there is output value every element of input argument, i. Input elements that do not match a named element of x return NA. Because of the indeterminant number of matches given a pattern search against a searchable object, there is no guarantee that a search pattern have a match. If no matches are found, a zero-length object is returned. (This may change to NA to be more consisitent.)

Results do not yield a Searchable object, but the superclass that the searchable class wraps. See Value below.

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. } # ATOMIC VECTORS: v <- c( a=1, b=2, B=3, c=4, c2=5 ) sv <- searchable(v)

# FLEXIBLY FIND ELEMENTS BY NAME sv[ regex('c') ] sv[ fixed('c') ]

sv[ ignore.case('b') ]

# FLEXIBLY REPLACEMENT ELEMENTS BY NAME sv[ regex('c.?') ] <- "3rd"

# SET DEFAULT SEARCH FOR TARGET/OBJECT sv <- searchable(v, case_insensitive = TRUE ) sv['b'] sv['B']

sv <- regex(sv) sv['c']

sv <- ignore.case(sv) sv['b'] sv['c'] # st

# USE ON (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[.] extract stri_detect_regex reverse.lookup

Details

The searchable class allows 'stringr/i'-like searches using [ and [<- operators. The following search types are supported:

  • stdstandard R matching, the default
  • regexfor regular expression matching,
  • fixedfor fixed string matching,
  • collfor collation matching,

Class Searchable objects allow customizations of how R's [ operator match objects' names.