Learn R Programming

searchable (version 0.1.2)

[,searchable,character,missing-method: Extraction operators for searchable object

Description

Defines [, [[, and $ for searchable objects

Usage

## S3 method for class 'searchable,character,missing':
[(x, i, j, ..., drop = TRUE)

## S3 method for class 'searchable,character': [[(x, i)

## S3 method for class 'searchable': $(x, name)

## S3 method for class 'searchable,character,missing': [(x, i) <- value

## S3 method for class 'searchable,character,missing': [[(x, i) <- value

## S3 method for class 'searchable': $(x, name) <- value

Arguments

x
searchable object
i
character; pattern with potential match modifiers applied,
j
missing; never specified
...
additional arguments. See Extract
drop
For matrices and arrays. If TRUE the result is coerced to the lowest possible dimension (see the examples). This only works for extracting elements, not for the replacement. See drop for further details.
name
character; a name to be extracted, used with $, so no match modification can be applied to the name.
value
replacement value for replacement functions

The methods for searching respect the modifiers applied to both x and i.

Value

  • The values after the extracting methods have been applied: [ returns a subset of x, but which is not searchable. [[ returns an element of x $ retutrns an element of x

<code>[</code>, <code>[&lt;-</code>

[ and [<- are used for getting and setting zero or more elemenxts of x. When searching using a pattern, this is generally the search type that is wanted.

[ does not return a searchable object. It is generally thought that the returned object should not be a returnable object.

<code>[[</code>, <code>[[&lt;-</code> and <code>$</code>, <code>$&lt;-</code>

These operators are used for getting and setting at zero or one element of x. Matches to more than one element result in an error.

repeated-names

Unlike for environments or hashes, there are no constraints ensuring uniqueness for names in vectors and lists. These structures may contain multiple elements with the same name. Attempts to extract by this name yield the first occurence of the name.

See Also

searchable 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[ c('a','b') ]        # Normal
    sv[ perl('c.?') ]
    sv[ fixed('c') ]
    sv[ 'x' ]                # NA

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

    sv$a
    sv$b
    sv$B


  # WITH MARGRITTR:
  "b" %>% sv[[.]]
    "B" %>% ignore.case %>% sv[.]
    "c." %>% perl %>% sv[[.]]
    "c.?" %>% perl %>% sv[.]

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


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

Run the code above in your browser using DataLab