searchable: Searchable

Description Usage Arguments Details Value Diffences from stringr Differences from base R replacement multiple dimension objects Note See Also Examples

Description

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

Usage

1
2
3
4
searchable(object, type = "std", ...)

## S4 method for signature '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.

Details

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

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

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:

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.

Note

- Environments cannot be (easily) be made "searchable" due to the way the they are implemented.

- The extraction methods for searchable objects are (at present) limited to only one pattern. This may change in the future.

See Also

extract
stri_detect_regex
reverse.lookup

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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
   ## Not run: 
    sl[ "B"  %>% ignore.case ]
    "b" %>% sl[.]
    "B" %>% ignore.case %>% sl[.]
   
## End(Not run)

searchable documentation built on May 1, 2019, 9:45 p.m.