Skip to contents

Creates a factory function which returns a different chunk of a given data object with each function call.

Usage

chunk_data_(x, size = 10, reverse = FALSE)

Arguments

x

A data frame or vector.

size

An integer. The number of items (e.g. rows in a tibble) that make up a given chunk. Must be a positive integer.

reverse

A logical. Calculate chunks from back to front.

Value

A factory function which returns a chunk of data from the provided object with each call. Once all data has been returned, function returns NULL perpetually.

Examples

# Create chunk factory function
chunked_data <- chunk_data_(mtcars, size = 6)

# Chunk #1 (rows 1-6)
paste0(rownames(chunked_data()), collapse = ', ')
#> [1] "Mazda RX4, Mazda RX4 Wag, Datsun 710, Hornet 4 Drive, Hornet Sportabout, Valiant"

# Chunk #2 (rows 7-12)
paste0(rownames(chunked_data()), collapse = ', ')
#> [1] "Duster 360, Merc 240D, Merc 230, Merc 280, Merc 280C, Merc 450SE"