◆ Reference · R
Reference › R
R
The milkflow package (extendr) is a thin layer over the core's JSON door. It depends only on jsonlite; sf, htmltools and knitr are optional. Building from source needs Rust 1.85 or later.
install.packages("milkflow_0.1.0.tar.gz", repos = NULL, type = "source")
The package has a site of its own, with the help page of every function and the vignette with its figures: milkflow for R.
Example
library(milkflow)
nodes <- data.frame(
id = c("centro", "porto", "serra", "olaria"),
x = c(0, 3200, -3800, -400),
y = c(0, -1400, 3600, -3900),
label = c("Centro", "Porto", "Serra", "Olaria")
)
flows <- data.frame(
origin = c("serra", "porto", "olaria", "centro", "serra"),
destination = c("centro", "centro", "centro", "porto", "porto"),
value = c(410, 260, 180, 90, 35)
)
m <- flowmap(nodes, flows, k = 2)
write_svg(m, "mapa.svg")
m # the accounting: in corridors, internal, filtered, without a path
#> <milkflow> 4 places (from 4 nodes), 3 segments, 2 width classes
#> Volume: 975 in 5 pairs (5 routed)
#> in corridors 100.0%
#> internal 0.0%
#> filtered out 0.0%
#> without a path 0.0%
#> Detour: mean 1.00, max 1.01
Functions
| Function | What it does |
|---|---|
flowmap(nodes, flows, ..., skeleton, boundary, crs, units, group, k, filter, mu, max_stretch, skeleton_source, represent, schematic, schematic_options, tikz, html, lines, max_categories, style, id, x, y, origin, destination, value, category, label, weight) | Draws the map. nodes is a data frame or an sf object of points in a projected CRS (lon/lat is refused). group takes TRUE (column "group") or the name of a column, passed to the core as a level of groups: a table with one column per level (district, region) gives the figure of each level, call by call, and x$spec$meta$params$group records the level. filter is a name ("dominant") or a list (list(kind = "disparity", alpha = 0.01)); schematic_options and style are named lists with the fields of the reference; category names the category column of the flows (the lines mode). Returns a milkflow object: a list with spec (tables as data frames; in the lines mode, spec$lines in long form), svg, schematic_spec, schematic_svg, schematic_error, tikz, schematic_tikz, html and json. Warnings of the core (repeated pairs summed, nodes merged, categories merged into “other”, the fallback slack of the schematic layout, long detours with a sparse filter) become a single warning of class milkflow_warning, one line per warning. |
write_svg(x, file, schematic = FALSE) | Writes the figure. |
write_tikz(x, file, schematic = FALSE) | Writes the tikzpicture (needs tikz = TRUE). |
write_html(x, file) | Writes the interactive page (needs html = TRUE). |
plot(x, schematic = FALSE) | Shows it in the RStudio Viewer or the browser. |
print(x), format(x) | The summary: places, segments, classes and the volume partition; in the lines mode, the categories; the junctions, when there are any; the detour; the outputs asked for (TikZ, interactive page), the state of the schematic layout and the warnings of the core. |
flowmap_json(input, options = "") | The raw door: JSON text in and out. |
new_milkflow(json) | Builds the milkflow object from the output of flowmap_json(). |
demo_nodes, demo_flows, demo_boundary | The package's example data. |
Core errors (unknown ids, non-finite values, invalid options) are conditions of class milkflow_error. In knitr documents with HTML output the object shows itself as the figure.