insertText(location, text, id = NULL)modifyRange(location, text, id = NULL)
setDocumentContents(text, id = NULL)
ranges list.NULL or blank,
the mutation will apply to the currently open, or last
focused, RStudio document. Use the id returned
from getActiveDocumentContext() to ensure
that the operation is applied on the intended document.location should be a (list of) document_position or
document_range object(s), or numeric vectors coercable to
such objects. To operate on the current selection in a document, call insertText()
with only a text argument, e.g.
insertText("# Hello\n")
insertText(text = "# Hello\n")
Otherwise, specify a (list of) positions or ranges, as in:
# insert text at the start of the document
insertText(c(1, 1), "# Hello\n") # insert text at the end of the document
insertText(Inf, "# Hello\n") # comment out the first 5 rows
pos <- Map(c, 1:5, 1)
insertText(pos, "# ") # uncomment the first 5 rows, undoing the previous action
rng <- Map(c, Map(c, 1:5, 1), Map(c, 1:5, 3))
modifyRange(rng, "")
modifyRange is a synonym for insertText, but makes its intent
clearer when working with ranges, as performing text insertion with a range
will replace the text previously existing in that range with new text. For
clarity, prefer using insertText when working with
document_positions, and modifyRange when working with
document_ranges.