Use these functions to interact with documents open in RStudio.
insertText(location=NULL, text=NULL, id=NULL)modifyRange(location=NULL, text=NULL, id=NULL)setDocumentContents(text, id=NULL)setCursorPosition(position, id=NULL)setSelectionRanges(ranges, id=NULL)documentId(allowConsole=TRUE)documentPath(id=NULL)documentSave(id=NULL)documentSaveAll()documentNew(text, type=c("r","rmarkdown","sql"), position=document_position(0,0), execute=FALSE)documentOpen(path, line=-1L, col=-1L, moveCursor=TRUE)documentClose(id=NULL, save=TRUE)An object specifying the positions, or ranges, wherein textshould be inserted. SeeDetails for more information.
A character vector, indicating what text should be inserted ateach aforementioned range. This should either be length one (in which case,this text is applied to each range specified); otherwise, it should be thesame length as theranges list.
The document id. WhenNULL or blank, the requested operationwill apply to the currently open, or last focused, RStudio document.
The cursor position, typically created throughdocument_position().
A list of one or more ranges, typically createdthroughdocument_range().
Allow the pseudo-id#console to be returned, if theRconsole is currently focused? Set this toFALSE if you'd always like totarget the currently-active or last-active editor in the Source pane.
The type of document to be created.
Should the code be executed after the documentis created?
The path to the document.
The line in the document to navigate to.
The column in the document to navigate to.
Boolean; move the cursor to the requested location afteropening the document?
Whether to commit unsaved changes to the document before closing it.
location should be a (list of)document_position ordocument_range object(s), or numeric vectors coercable tosuch objects.
To operate on the current selection in a document, callinsertText()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 documentinsertText(c(1,1),"# Hello\n")# insert text at the end of the documentinsertText(Inf,"# Hello\n")# comment out the first 5 rowspos<-Map(c,1:5,1)insertText(pos,"# ")# uncomment the first 5 rows, undoing the previous actionrng<-Map(c,Map(c,1:5,1),Map(c,1:5,3))modifyRange(rng,"")modifyRange is a synonym forinsertText, but makes its intentclearer when working with ranges, as performing text insertion with a rangewill replace the text previously existing in that range with new text. Forclarity, prefer usinginsertText when working withdocument_positions, andmodifyRange when working withdocument_ranges.
documentClose accepts an ID of an open document rather than a path.You can retrieve the ID of the active document using thedocumentId()function.
Closing is always done non-interactively; that is, no prompts are given tothe user. If the user has made changes to the document but not saved them,then thesave parameter governs the behavior: whenTRUE,unsaved changes are committed, and whenFALSE they are discarded.
TheinsertText,modifyRange andsetDocumentContentsfunctions were added with version 0.99.796 of RStudio.
ThesetCursorPosition andsetSelectionRanges functions wereadded with version 0.99.1111 of RStudio.
ThedocumentSave anddocumentSaveAll functions were addedwith version 1.1.287 of RStudio.
ThedocumentId anddocumentPath functions were added withversion 1.4.843 of RStudio.
ThedocumentNew function was introduced in RStudio 1.2.640.
ThedocumentOpen function was introduced in RStudio 1.4.1106.
ThedocumentClose function was introduced in RStudio 1.2.1255