CRDTs for R

Conflict-Free Data Structures for Real-Time Collaboration

Charlie Gao

Posit PBC

Where does R live in
collaborative work?

“Send me the file, I’ll merge it”

  • Two people, one model_final_v3.R — who wins?
  • Locking — everyone else waits
  • Central arbitration — a server orders every change
  • Last write wins — someone’s work vanishes


Concurrency — every answer requires a coordinator

CRDTs

A data structure whose concurrent operations
converge to the same state — in any order


Strong eventual consistency: a property, not a merge strategy

Automerge is a specification

  • A document model — maps, lists, text, counters
  • A binary format — for documents and every change
  • Implementations: Rust, JavaScript … now R


Same bytes, any language

automerge — CRDTs as R objects

library(automerge)

# Two researchers working independently
alice <- am_create()
alice$experiment <- "trial_001"
alice$temperature <- 23.5

bob <- am_create()
bob$humidity <- 65

# Later: one call, zero conflicts
am_sync(alice, bob)

# both documents now hold all three fields

Every edit is an operation

  • Documents carry their whole history
  • Concurrent text edits merge at character level
  • Who changed what, when — attribution built in

Syncing is also just a protocol

automerge-repo — peers announce, request,
and sync documents over WebSockets


Speak the wire format → join the network

autosync — R speaks the protocol

  • R as sync server and client — a peer
  • Interoperates live with JavaScript, Rust, sync.automerge.org
  • TLS + OAuth/OIDC authentication on the wire


WebSockets over nanonext — the async foundation that powers mirai

Demo

library(shinysync)
library(autosync)
library(automerge)

project_app(
  "wss://quarto-hub.com/ws",
  proj_id = "<project-id>"
)


Left: an R session · Right: a browser

Same document — both directions — live

Multiplayer R


R as a first-class, authenticated peer

What the concept opens up

  • Multi-site data collection or labelling — merged, with attribution
  • A Shiny app whose state is a json string in CRDT — for instruction, or multi-party input

Bigger than one package

  • Local-first is a movement — Automerge, Ink & Switch, a growing ecosystem
  • Key: the concept of CRDTs for shared state

Thanks

automergeposit-dev/automerge-r · CRAN

autosyncposit-dev/autosync · CRAN

shinysyncshikokuchuo/shinysync

automerge.org — the specification & ecosystem


Charlie Gao · Posit PBC