R: Callback classes

callback {readr} | R Documentation

## Callback classes

### Description

These classes are used to define callback behaviors.

### Details

ChunkCallback

  Callback interface definition, all callback functions should inherit from this class.

SideEffectChunkCallback

  Callback function that is used only for side effects, no results are returned.

DataFrameCallback

  Callback function that combines each result together at the end.

AccumulateCallBack

  Callback function that accumulates a single result. Requires the parameter `acc` to specify the initial value of the accumulator. The parameter `acc` is `NULL` by default.

### Methods

#### Public methods

- `ChunkCallback$new()`

- `ChunkCallback$receive()`

- `ChunkCallback$continue()`

- `ChunkCallback$result()`

- `ChunkCallback$finally()`

- `ChunkCallback$clone()`

#### Method `new()`

##### Usage

    ChunkCallback$new(callback)

#### Method `receive()`

##### Usage

    ChunkCallback$receive(data, index)

#### Method `continue()`

##### Usage

    ChunkCallback$continue()

#### Method `result()`

##### Usage

    ChunkCallback$result()

#### Method `finally()`

##### Usage

    ChunkCallback$finally()

#### Method `clone()`

The objects of this class are cloneable with this method.

##### Usage

    ChunkCallback$clone(deep = FALSE)

##### Arguments

`deep`

  Whether to make a deep clone.

### Super class

`readr::ChunkCallback` -> `SideEffectChunkCallback`

### Methods

#### Public methods

- `SideEffectChunkCallback$new()`

- `SideEffectChunkCallback$receive()`

- `SideEffectChunkCallback$continue()`

- `SideEffectChunkCallback$clone()`

Inherited methods

- `readr::ChunkCallback$finally()`

- `readr::ChunkCallback$result()`

#### Method `new()`

##### Usage

    SideEffectChunkCallback$new(callback)

#### Method `receive()`

##### Usage

    SideEffectChunkCallback$receive(data, index)

#### Method `continue()`

##### Usage

    SideEffectChunkCallback$continue()

#### Method `clone()`

The objects of this class are cloneable with this method.

##### Usage

    SideEffectChunkCallback$clone(deep = FALSE)

##### Arguments

`deep`

  Whether to make a deep clone.

### Super class

`readr::ChunkCallback` -> `DataFrameCallback`

### Methods

#### Public methods

- `DataFrameCallback$new()`

- `DataFrameCallback$receive()`

- `DataFrameCallback$result()`

- `DataFrameCallback$finally()`

- `DataFrameCallback$clone()`

Inherited methods

- `readr::ChunkCallback$continue()`

#### Method `new()`

##### Usage

    DataFrameCallback$new(callback)

#### Method `receive()`

##### Usage

    DataFrameCallback$receive(data, index)

#### Method `result()`

##### Usage

    DataFrameCallback$result()

#### Method `finally()`

##### Usage

    DataFrameCallback$finally()

#### Method `clone()`

The objects of this class are cloneable with this method.

##### Usage

    DataFrameCallback$clone(deep = FALSE)

##### Arguments

`deep`

  Whether to make a deep clone.

### Super class

`readr::ChunkCallback` -> `ListCallback`

### Methods

#### Public methods

- `ListCallback$new()`

- `ListCallback$receive()`

- `ListCallback$result()`

- `ListCallback$finally()`

- `ListCallback$clone()`

Inherited methods

- `readr::ChunkCallback$continue()`

#### Method `new()`

##### Usage

    ListCallback$new(callback)

#### Method `receive()`

##### Usage

    ListCallback$receive(data, index)

#### Method `result()`

##### Usage

    ListCallback$result()

#### Method `finally()`

##### Usage

    ListCallback$finally()

#### Method `clone()`

The objects of this class are cloneable with this method.

##### Usage

    ListCallback$clone(deep = FALSE)

##### Arguments

`deep`

  Whether to make a deep clone.

### Super class

`readr::ChunkCallback` -> `AccumulateCallback`

### Methods

#### Public methods

- `AccumulateCallback$new()`

- `AccumulateCallback$receive()`

- `AccumulateCallback$result()`

- `AccumulateCallback$clone()`

Inherited methods

- `readr::ChunkCallback$continue()`

- `readr::ChunkCallback$finally()`

#### Method `new()`

##### Usage

    AccumulateCallback$new(callback, acc = NULL)

#### Method `receive()`

##### Usage

    AccumulateCallback$receive(data, index)

#### Method `result()`

##### Usage

    AccumulateCallback$result()

#### Method `clone()`

The objects of this class are cloneable with this method.

##### Usage

    AccumulateCallback$clone(deep = FALSE)

##### Arguments

`deep`

  Whether to make a deep clone.

### See Also

Other chunked: `melt_delim_chunked()`, `read_delim_chunked()`, `read_lines_chunked()`

### Examples

    ## If given a regular function it is converted to a SideEffectChunkCallback

    # view structure of each chunk
    read_lines_chunked(readr_example("mtcars.csv"), str, chunk_size = 5)

    # Print starting line of each chunk
    f <- function(x, pos) print(pos)
    read_lines_chunked(readr_example("mtcars.csv"), SideEffectChunkCallback$new(f), chunk_size = 5)

    # If combined results are desired you can use the DataFrameCallback

    # Cars with 3 gears
    f <- function(x, pos) subset(x, gear == 3)
    read_csv_chunked(readr_example("mtcars.csv"), DataFrameCallback$new(f), chunk_size = 5)

    # The ListCallback can be used for more flexible output
    f <- function(x, pos) x$mpg[x$hp > 100]
    read_csv_chunked(readr_example("mtcars.csv"), ListCallback$new(f), chunk_size = 5)

    # The AccumulateCallback accumulates results from each chunk
    f <- function(x, pos, acc) sum(x$mpg) + acc
    read_csv_chunked(readr_example("mtcars.csv"), AccumulateCallback$new(f, acc = 0), chunk_size = 5)

[Package readr version 2.1.2 Index]