dot: Create an iterable object

Description Usage Arguments Details Value Examples

Description

As S3 generic to create a subclass of IterateR. Object that are subclasses of IterateR have numberous reference class methods defined for them, though the primary one is each.

Usage

1
  .(x, mode, ...)

Arguments

x

object to turn into an iterable object.

mode

If x is unspecified and modes are specified then objects in the global workspace of that mode are used to iterate over. The value is passed to mget to get the objects.

...

passed to new of IterateR subclass. Used by ObjectIterateR, for example, to pass in information to ls.

Details

The each method allows a simple means to iterate over the objects. The functionality is very similar to sapply, say, but has some novelties: the function arguments determine what gets passed in. If the function has arguments x, i or nm then these are passed the object, its index, or name, respectively.

Other methods include:

split to split the objects before iterating

filter to apply a function to filter the objects by

find to apply R's Find function

position to apply R's Position function

tail to call tail

head to call head

slice to call [

Each of the above returns an instance of a subclass of IterateR allowing them to be chained together in one call.

The core method returns the underlying object. If simplify=TRUE, the default, then this is after calling simplify2array.

The notation reverses that of jQuery where $(obj).method is replaced in R with .(obj)$method. Okay, cute but is it of any use? Maybe not.

Value

Returns an instance of a subclass of IterateR.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## just like sapply(mtcars, median)
.(mtcars)$each(median) ## subclass of IterateR
## call core to get objects:
.(mtcars)$each(median)
## can chain calls if you want.
.(iris)$filter(is.numeric)$each(median)
## Sometimes we want both index and value (e.g., mapply(f, mtcars, seq_len(nrow(mtcars))))
.(mtcars)$each(function(x, i) sprintf('Column %s has max %s', i, max(x)))
## lists have a pluck method
.(mtcars)$each(function(x) c(mean=mean(x), sd=sd(x))) ## a table, but really stored as a list
.(mtcars)$each(function(x) c(mean=mean(x), sd=sd(x)))$pluck("mean") ## just mean values

jverzani/iterateR documentation built on May 20, 2019, 5:20 a.m.