Learn R Programming

automerge (version 0.4.0)

am_text_update: Update text content

Description

An optimized function for collaborative editing that reads the current text content from the document, computes the minimal diff against the new text, and applies it directly. This avoids intermediate R object allocation, making it more efficient than separate diff computation and splice operations.

Usage

am_text_update(text_obj, new_text)

Value

Invisible NULL (called for side effect)

Arguments

text_obj

An Automerge text object ID

new_text

The new text content (single string)

Details

Positions use Unicode code points (matching R's nchar() behavior), not bytes. This means multibyte characters like emoji count as single characters.

Examples

Run this code
doc <- am_create()
am_put(doc, AM_ROOT, "content", am_text("Hello"))
text_obj <- am_get(doc, AM_ROOT, "content")
text_obj

# Efficiently update text by computing and applying diff in one step
am_text_update(text_obj, "Hello World")
am_text_content(text_obj)  # "Hello World"

# Works with Unicode
am_text_update(text_obj, "Hello World!")
am_text_content(text_obj)  # "Hello World!"

am_close(doc)

Run the code above in your browser using DataLab