Use these functions to interact with documents open in RStudio.
Creates a new document in RStudio
Closes a document currently open in RStudio.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
insertText(location, text, id = NULL) modifyRange(location, text, id = NULL) setDocumentContents(text, id = NULL) setCursorPosition(position, id = NULL) setSelectionRanges(ranges, id = NULL) documentSave(id = NULL) documentSaveAll() documentNew(text, type = c("r", "rmarkdown", "sql"), position = document_position(0, 0), execute = FALSE) documentClose(id = NULL, save = TRUE)
An object specifying the positions, or ranges, wherein text should be inserted. See Details for more information.
A character vector, indicating what text should be
inserted at each aforementioned range. This should either
be length one (in which case, this text is applied to each
range specified); otherwise, it should be the same length
The document id. When
The cursor position, typically created through
A list of one or more ranges, typically created
The type of document to be created.
Should the code be executed after the document is created?
Whether to commit unsaved changes to the document before closing it.
location should be a (list of)
document_range object(s), or numeric vectors coercable to
To operate on the current selection in a document, call
with only a text argument, e.g.
Otherwise, specify a (list of) positions or ranges, as in:
1 2 3 4 5 6 7 8 9 10 11 12 13
# 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
modifyRange when working with
documentClose accepts an ID of an open document rather than a path.
You can get the ID of an open document from the
getSourceEditorContext function, among others.
Closing is always done non-interactively; that is, no prompts are given to
the user. If the user has made changes to the document but not saved them,
save parameter governs the behavior: when
unsaved changes are committed, and when
FALSE they are discarded.
functions were added with version 0.99.796 of RStudio.
setSelectionRanges functions were
added with version 0.99.1111 of RStudio.
documentSaveAll functions were added
with version 1.1.287 of RStudio.
documentNew function was introduced in RStudio 1.2.640
documentClose function was introduced in RStudio 1.2.1255
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.