Skip to contents

R bindings for rpic — a Rust reimplementation of Brian Kernighan’s pic picture-drawing language. You describe a diagram by walking around a plane dropping primitives; rpic renders it to SVG / PNG / PDF, pure Rust, no system dependencies (no troff, no LaTeX, no ImageMagick):

  • 79 native circuit elements — a from-scratch re-imagining of circuit_macros (circuits = TRUE or copy "circuits" in the source);
  • TeX math labels typeset natively (texlabels = TRUE, KaTeX-grade via a pure-Rust engine);
  • structured diagnostics — compile errors are classed conditions with exact positions and did-you-mean hints;
  • a knitr engine for inline diagrams in R Markdown / Quarto.

rpic follows the grammar and behaviour of dpic by J. D. (Dwight) Aplevich, the reference implementation of pic, and its circuit library is modelled on his circuit_macros.

An independent project, not affiliated with, maintained by or endorsed by the authors of pic, dpic, circuit_macros or pikchr. rpic was written with the assistance of AI coding tools.

The language, its extensions and a live playground are documented at rpic.dev; this package wraps the same engine via extendr.

Install

# install.packages("remotes")
remotes::install_github("milkway/rpic-r")

Building from source requires a Rust toolchain (cargo/rustc). Release tarballs with vendored Rust dependencies (no network needed at build time) are attached to each GitHub release.

Usage

library(rpic)

flow <- pic('box "input"; arrow; box "process" fill 0.9; arrow; ellipse "output"')
flow |> rpic_svg()
flow |> rpic_png("flow.png", scale = 2)

# circuit library — two-terminal elements take two named points; pic()
# notices the element call and loads the library (circuits = TRUE):
pic('A:(0,0); B:(2,0)
resistor(A,B)') |>
  rpic_pdf("circuit.pdf")

# TeX math labels, typeset natively (texlabels detected from the `$…$`):
pic('box "$-\\frac{T}{2}$" fit') |> rpic_svg()

# drawing data as data frames: objects, named places, warnings
flow |> rpic_objects()

# maps: georeference the drawing as an sf data frame, or put it on mapgl
pic('geo_lon = -34.8711; geo_lat = -8.0631; geo_scale = 1000
circle rad 2 at (0, 0) fill 0.95') |>
  rpic_sf()

Every function also takes a plain string of pic source.

Errors you can point at

Compile failures raise a classed rpic_error condition carrying the structured diagnostic — position (always relative to your source, even with circuits = TRUE), kind, and a did-you-mean hint:

tryCatch(
  pic("bxo", circuits = TRUE) |> rpic_svg(),
  rpic_error = function(e) list(line = e$info$line, hint = e$info$hint)
)
#> $line
#> [1] 1
#> $hint
#> [1] "did you mean `box`?"

e$info$file names a copy include when the problem is inside one (NA means your own input).

knitr / Quarto engine

then write pic code directly in a chunk:

```{rpic, circuits=TRUE, scale=2}
A:(0,0); B:(2,0)
resistor(A,B)
```

Chunk options: circuits, texlabels, scale.

Develop

devtools::load_all(".")      # compiles the Rust and loads the package

For an installable/CRAN-style tarball, the Rust dependencies are vendored:

rextendr::vendor_crates(".")   # bundles crate sources into src/rust/vendor.tar.xz
R CMD build .

Acknowledgments

pic was created by Brian W. Kernighan; dpic and circuit_macros are J. D. Aplevich’s; pikchr is D. Richard Hipp’s. See ACKNOWLEDGMENTS in the engine repository.

License

BSD-2-Clause. Compiled Rust dependencies are acknowledged in inst/AUTHORS.