Skip to contents

r8map(x, f) calls f once for each item of x, in order, and collects the results in the container that f's return type declares. r8map() is a generic of R8 that dispatches on x and on what f is: a method for Raven applies to every table.

Usage

r8map(
  x,
  f,
  columns = NULL,
  by = NULL,
  args = NULL,
  n_threads = NULL,
  verbosity = 1L
)

Arguments

x

A value of a table.

f

A typed function, whose first parameter takes each item, or, for a table, a calc() that reads col().

columns

Optional Character or numeric: The columns mapped, by name or by position, each once.

by

Optional Character or numeric: The key columns, by name or by position.

args

Optional named list: The constant arguments of every call of a typed function, by the names of its parameters other than the first; on a table, one value each.

n_threads

Optional Integer [1, Inf): The threads that the work in Rust may use; NULL takes getOption("r8.n_threads"), whose default is half the logical cores, at most 2 where R CMD check limits the cores, and at most OMP_THREAD_LIMIT. A result is the same for any number of threads. A typed function with its body in R runs on R's thread.

verbosity

Integer [0, Inf): 0 writes no message, and 1 the start and the end of a map that takes longer than 0.2 s.

Value

Of a table, a Raven of the key columns and one column for each column mapped; of a list or a vector, an atomic vector or a list, with the names of x.

Lists and vectors

The items of a list or an atomic vector are its elements, given to the first parameter of a typed function (new_function()); args holds the constant arguments of every call, by the names of f's other parameters: r8map(folds, train_glm, args = list(data = d, outcome = "y")). A scalar return type gives an atomic vector, and another type a list of the values, checked by f, with the names of x. An error of a call names its item, and the map gives nothing.

Progress

A map of a typed function reports its progress to a host that sets the option r8.progress to a list of three functions: begin(total, label, kind), which gives a handle, update(handle, current), and end(handle, status), with the status "completed", "failed", or "aborted". The total is the calls of f, the label its name, and the kind "r8map"; on a table, the total is the groups times the columns. begin is called once the map has run for 0.2 s, so that a shorter map reports nothing, and update about every 0.05 to 0.2 s. Without the option, its default, a map reports nothing. A hook with the function stages(handle, plan) receives, before end, the plan of the map's run, the r8_plan of r8explain() with each stage's status and times, as r8bench() collects it.

Messages

At verbosity 1 or more, a map that takes longer than 0.2 s writes a start line once it has run for 0.2 s and an end line when it completes, each with the time and where the map runs, in Rust, in R, or both, by the bodies of its typed functions; the end line states the wall time, the CPU time of the process, and the threads that the map may use: 2026-11-27 10:04:15 r8map(): completed in 3.3 s, CPU 3.2 s of 1 thread, in Rust [r8map]. The lines are messages, which suppressMessages() silences. A hook in r8.progress with a fourth function, message(event), receives each as a list of level, phase ("start" or "end"), text, caller, and time; a hook without one receives begin and end in their place.

Tables

r8map(x, f, columns, by) gives one row for each group of the rows of x by the key columns by, or one row for the table: the keys, then f of each column of columns, under the column's name. Without columns, every column that is not a key is mapped. r8map(split(x, x[["g"]]), f) maps f over the tables of the groups.

Typed functions

A typed function of R8 (new_function()), with a body in R or in Rust, is called on the cells of each column in each group, as a direct call is, and returns one value of its declared return type: r8map(metrics, trimmed_mean, columns = metric_names, by = "param_combo_id"). The map is that of calc(f(col())): a body in Rust is called from R8's Rust code, and a body in R in one loop of R for each column. Its arguments are checked as those of a direct call: a group with a missing cell is an error unless the parameter's type is option(). On a table, args gives one value to each of f's other parameters, args = list(trim = 0.2). Moving its body from R to Rust changes no call.

Expressions

A calc() in which col() is the column mapped is evaluated in Rust, as the summaries of r8summarize() are: r8map(metrics, calc(median(col())), by = "param_combo_id"). Its results have the units that the expression infers.

A plain R function is an error that names new_function(): lapply(x[columns], f) applies one to whole columns, and split() gives the groups' tables.

Examples

op <- options(r8.namespace = "demo")
m <- Raven(
  combo = c(1L, 1L, 2L, 2L),
  auc = c(0.81, 0.79, 0.85, 0.88),
  brier = c(0.16, 0.18, 0.14, 0.12)
)
r8map(m, calc(median(col())), by = "combo")
#> <raven table> 2 rows ⨉ 3 columns
#>      combo     auc   brier
#>    integer  double  double
#> 1        1   0.800    0.17
#> 2        2   0.865    0.13
spread <- new_function(
  "spread",
  function(x = arg_vec(type_double())) max(x) - min(x),
  return_type = type_double()
)
r8map(m, spread, columns = "auc", by = "combo")
#> <raven table> 2 rows ⨉ 2 columns
#>      combo     auc
#>    integer  double
#> 1        1    0.02
#> 2        2    0.03
# A resample is the indices of its training rows; the data is a constant.
Fold <- new_struct("Fold", fields = list(train = vec(type_integer())))
folds <- list(a = Fold(train = 1:2), b = Fold(train = 3:4))
fold_auc <- new_function(
  "fold_auc",
  function(fold = arg(Fold), data = arg(Raven)) mean(data[fold$train, ]$auc),
  return_type = type_double()
)
r8map(folds, fold_auc, args = list(data = m))
#>     a     b 
#> 0.800 0.865 
options(op)