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.

See also

Other 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)
#> chr [1:5] "\"mpg\",\"cyl\",\"disp\",\"hp\",\"drat\",\"wt\",\"qsec\",\"vs\",\"am\",\"gear\",\"carb\"" ... #> chr [1:5] "18.7,8,360,175,3.15,3.44,17.02,0,0,3,2" ... #> chr [1:5] "19.2,6,167.6,123,3.92,3.44,18.3,1,0,4,4" ... #> chr [1:5] "10.4,8,472,205,2.93,5.25,17.98,0,0,3,4" ... #> chr [1:5] "33.9,4,71.1,65,4.22,1.835,19.9,1,1,4,1" ... #> chr [1:5] "19.2,8,400,175,3.08,3.845,17.05,0,0,3,2" ... #> chr [1:3] "19.7,6,145,175,3.62,2.77,15.5,0,1,5,6" ...
#> NULL
# 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)
#> [1] 1 #> [1] 6 #> [1] 11 #> [1] 16 #> [1] 21 #> [1] 26 #> [1] 31
#> NULL
# 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)
#> Parsed with column specification: #> cols( #> mpg = col_double(), #> cyl = col_integer(), #> disp = col_integer(), #> hp = col_integer(), #> drat = col_double(), #> wt = col_double(), #> qsec = col_double(), #> vs = col_integer(), #> am = col_integer(), #> gear = col_integer(), #> carb = col_integer() #> )
#> # A tibble: 15 × 11 #> mpg cyl disp hp drat wt qsec vs am gear carb #> * <dbl> <int> <int> <int> <dbl> <dbl> <dbl> <int> <int> <int> <int> #> 1 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 #> 2 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 #> 3 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 #> 4 14.3 8 360 245 3.21 3.570 15.84 0 0 3 4 #> 5 16.4 8 NA 180 3.07 4.070 17.40 0 0 3 3 #> 6 17.3 8 NA 180 3.07 3.730 17.60 0 0 3 3 #> 7 15.2 8 NA 180 3.07 3.780 18.00 0 0 3 3 #> 8 10.4 8 472 205 2.93 5.250 17.98 0 0 3 4 #> 9 10.4 8 460 215 3.00 5.424 17.82 0 0 3 4 #> 10 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4 #> 11 21.5 4 NA 97 3.70 2.465 20.01 1 0 3 1 #> 12 15.5 8 318 150 2.76 3.520 16.87 0 0 3 2 #> 13 15.2 8 304 150 3.15 3.435 17.30 0 0 3 2 #> 14 13.3 8 350 245 3.73 3.840 15.41 0 0 3 4 #> 15 19.2 8 400 175 3.08 3.845 17.05 0 0 3 2
# 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)
#> Parsed with column specification: #> cols( #> mpg = col_double(), #> cyl = col_integer(), #> disp = col_integer(), #> hp = col_integer(), #> drat = col_double(), #> wt = col_double(), #> qsec = col_double(), #> vs = col_integer(), #> am = col_integer(), #> gear = col_integer(), #> carb = col_integer() #> )
#> [[1]] #> [1] 21.0 21.0 21.4 18.7 #> #> [[2]] #> [1] 18.1 14.3 19.2 #> #> [[3]] #> [1] 17.8 16.4 17.3 15.2 10.4 #> #> [[4]] #> [1] 10.4 14.7 #> #> [[5]] #> [1] 15.5 15.2 13.3 19.2 #> #> [[6]] #> [1] 30.4 15.8 19.7 #> #> [[7]] #> [1] 15.0 21.4 #>