milkflow is one program with several doors. The computation lives in
a Rust library, the crate milkflow; the R package, the
Python package, the JavaScript package and the command line are thin
layers over it. This vignette shows what that means for an R user: what
the package does itself, what it hands over, and what comes back.
One core, four doors
The bindings talk to the core through a single function,
run_json: two pieces of JSON text in (the input and the
options), one JSON object out. Nothing else crosses the border, so there
is no second implementation of the algorithm to drift away from the
first. The consequence is the package’s main promise: the same
input and options give the same bytes in R, in Python, in the
browser and at the command line.
The Rust sources travel inside the R package (the core crate and its
dependencies, in src/rust/vendor.tar.xz) and are compiled
when the package is installed. That is why building from source needs
Rust, and why nothing is downloaded at install time.
What one call does
flowmap() does three things on the R side:
- it checks the data frames (or
sfobjects) and finds the columns; - it writes the input and the options as JSON text;
- it parses the JSON object that comes back into a
milkflowobject.
Everything between steps 2 and 3 happens in the core. The two halves
are exported on their own, as the raw door: flowmap_json()
takes and returns JSON text, and new_milkflow() builds the
object from that text.
input <- '{
"nodes": [{"id": "a", "x": 0, "y": 0}, {"id": "b", "x": 4, "y": 7},
{"id": "c", "x": -6, "y": -9}],
"flows": [{"origin": "c", "destination": "a", "value": 410},
{"origin": "b", "destination": "a", "value": 260},
{"origin": "c", "destination": "b", "value": 35}]
}'
json <- flowmap_json(input, '{"k": 0}')
substr(json, 1, 60)
#> [1] "{\"spec\":{\"version\":\"0.1\",\"crs\":null,\"units\":null,\"meta\":{\"pa"
raw <- new_milkflow(json)
print(raw)
#> <milkflow> 3 places (from 3 nodes), 2 segments, 1 width classes
#> Volume: 705 in 3 pairs (3 routed)
#> in corridors 100.0%
#> internal 0.0%
#> filtered out 0.0%
#> without a path 0.0%
#> Detour: mean 1.00, max 1.00The same three places through flowmap() give the same
object, because flowmap() only writes that JSON for
you:
nodes <- data.frame(id = c("a", "b", "c"), x = c(0, 4, -6), y = c(0, 7, -9))
flows <- data.frame(origin = c("c", "b", "c"), destination = c("a", "a", "b"),
value = c(410, 260, 35))
m <- flowmap(nodes, flows, k = 0)
identical(m$svg, raw$svg)
#> [1] TRUEThe raw door is useful when the input already exists as JSON (written
by another program, or by the command line), and for checking a figure
against another language: the text that flowmap_json()
returns is what milkflow.run_json() returns in Python for
the same two arguments.
What comes back
A milkflow object is a list. Its fields are the fields
of the core’s JSON object:
names(m)
#> [1] "spec" "svg" "schematic_spec"
#> [4] "schematic_svg" "schematic_error" "tikz"
#> [7] "schematic_tikz" "geojson" "schematic_geojson"
#> [10] "html" "json"-
specis the drawing contract: what was computed, as data. It is the only field the package reshapes: its tables become data frames. -
svgis the corridor map, one string. It is always there. -
tikz,htmlandgeojsonareNULLunless asked for (tikz = TRUE,html = TRUE,geojson = TRUE); theschematic_*fields, unlessschematic = TRUE. -
jsonis the output of the core, verbatim.
str(m$spec, max.level = 1, give.attr = FALSE)
#> List of 8
#> $ version : chr "0.1"
#> $ crs : NULL
#> $ units : NULL
#> $ meta :List of 3
#> $ nodes :'data.frame': 3 obs. of 10 variables:
#> $ segments:'data.frame': 2 obs. of 7 variables:
#> $ legend :List of 1
#> $ boundary: list()
m$spec$segments[, c("id", "a", "b", "flow_ab", "flow_ba", "class")]
#> id a b flow_ab flow_ba class
#> 1 0 0 1 35 260 0
#> 2 1 0 2 0 445 0The drawings are made from the spec, by the core. A figure of your
own (with ggplot2, with a GIS, with another program) can start from the
same tables: nodes has the positions, segments
the paths and the volumes in each direction,
legend$width_classes the classes of width.
spec$meta records how the figure was made and what it
shows: the parameters of the run, the accounting of the volume and a few
metrics. print() formats the accounting:
m$spec$meta$accounting
#> $total
#> [1] 705
#>
#> $routed
#> [1] 705
#>
#> $internal
#> [1] 0
#>
#> $filtered
#> [1] 0
#>
#> $unroutable
#> [1] 0
#>
#> $pairs
#> [1] 3
#>
#> $pairs_routed
#> [1] 3Errors and warnings come from the core
The core validates the input and the options; the package turns what
it says into R conditions. An error is a condition of class
milkflow_error, and where the core names a flow or a node
by position, the message gives the row of your data frame:
bad <- rbind(flows, data.frame(origin = "a", destination = "zz", value = 1))
tryCatch(flowmap(nodes, bad), milkflow_error = function(e) conditionMessage(e))
#> [1] "`flows` row 4: unknown destination `zz`"The warnings of the core (a schematic layout that needed more slack,
an aggregation that stopped early, a sparse filter with long detours)
arrive as one R warning of class milkflow_warning, and stay
in x$spec$meta$warnings.
The same bytes everywhere
Nothing in the pipeline reads a clock, a locale or a random seed: a run is a function of its two JSON texts. The repository keeps reference cases with the SHA-256 of every output, and its continuous integration compares them on Linux, macOS and Windows, natively, in WebAssembly and in Python; the tests of this package compare the R outputs with the same references.
For your own work this means a figure can be regenerated years later, on another machine or in another language, and compared byte for byte, as long as the version of milkflow is the same.
How the core gets from the flows to the figure is the subject of the vignette “How a flow map is made”.