Different pipes. One standard.

Type to search this site.

Search runs in your browser against a static index built with this site (Pagefind). Nothing you type is sent to a server.

How do I run vectorised R over batches?

Available — v0.0.1 (experimental)rbatch

Vectorised R over batches

R is fast on vectors and slow when it is called once per record. In batch mode each line is a JSON array that becomes a data frame, so toupper, nchar and cut run once per batch. A second pipe does the same over batch files.

Available in v0.0.1: the files, README and tests are in examples/batch-r of the openflange-go repository. Commands run from the repository root.

Needs: Rscript with jsonlite.

A pipe is a data-processing step with declared inputs and outputs.

What goes in, what comes out

Input (stdin)
{"name":" ada ","score":95}
{"name":"bob","score":70}
Command
printf '%s\n' '{"name":" ada ","score":95}' '{"name":"bob","score":70}' \
  | openflange run ./examples/batch-r/lines --grant subprocess
Output (stdout)
{"name":"ADA","score":95,"nameLength":3,"grade":"A"}
{"name":"BOB","score":70,"nameLength":3,"grade":"C"}

Files and further commands

examples/batch-r/lines/OPENFLANGE.md — the batch declaration (excerpt)
cardinality: one-to-one
deterministic: true
batch:
  maxItems: 1000
  maxWaitMs: 200
examples/batch-r/lines/tidy.R
# SPDX-License-Identifier: Apache-2.0
# tidy.R — batch mode, delivery `lines`: one JSON array of records per line in,
# one JSON array per line out. Each batch becomes a data frame; trimws, toupper,
# nchar and cut run once per batch (vectorised), not once per record.
#
#   printf '%s\n' '[{"name":" ada ","score":95},{"name":"bob","score":70}]' | Rscript --vanilla tidy.R
#   [{"name":"ADA","score":95,"nameLength":3,"grade":"A"},{"name":"BOB","score":70,"nameLength":3,"grade":"C"}]
library(jsonlite)

tidy <- function(df) {
  df$name <- toupper(trimws(df$name))
  df$nameLength <- nchar(df$name)
  df$grade <- as.character(cut(df$score, c(-Inf, 75, 90, Inf), labels = c("C", "B", "A"), right = FALSE))
  df
}

con <- file("stdin", open = "r")
while (length(line <- readLines(con, n = 1, warn = FALSE)) > 0) {
  df <- fromJSON(line)                       # a batch: array of records -> data frame
  cat(toJSON(tidy(df), digits = NA), "\n", sep = "")   # one answer line per batch
  flush(stdout())
}
examples/batch-r/file/OPENFLANGE.md — the same pipe over batch files (excerpt)
batch:
  maxItems: 50000
  maxWaitMs: 1000
  delivery: file
examples/batch-r/file/tidy-file.R — the loop over batch files (excerpt)
con <- file("stdin", open = "r")
while (length(line <- readLines(con, n = 1, warn = FALSE)) > 0) {
  req <- fromJSON(line)
  df <- stream_in(file(req$input), verbose = FALSE)            # NDJSON file -> data frame
  stream_out(tidy(df), file(req$output), verbose = FALSE, digits = NA)
  cat('{"ok":true}\n')                                          # the batch is done
  flush(stdout())
}
Run the portable tests of both pipes
openflange test ./examples/batch-r/lines
openflange test ./examples/batch-r/file

What to notice

  • Two pipes do the same row-wise transformation. lines/ gets one JSON array per stdin line and answers with one per stdout line. file/ gets an NDJSON file per batch: its path arrives on stdin as {"input", "output", "items"} and the answer is {"ok":true}.
  • Batch boundaries are not semantic. The transformation is row-wise on purpose; the tests run it again with maxItems: 1 and expect the same output.
  • Do not compute something across a batch — a mean, a z-score, a rank. Batches are cut by size and time, so the answer would change from run to run. For a statistic over the whole input see the R summary example; for groups with meaning see the moving-window example.
  • Choose file when the tool wants a file (stream_in, pandas) or when a batch is too large to be comfortable as one line.
  • Not re-run for this page (no R on the machine that built it); the command and output are copied from the repository README. The idea is explained in Batch mode.