Use roxygen types in the editor
Source:vignettes/articles/roxygen-type-hints.Rmd
roxygen-type-hints.RmdOpen an R script in your workspace and enter these examples. The server reads the source comments directly, so you can try the editor actions before generating help or running the script.
Complete arguments and return values
Book <- R6::R6Class("Book", public = list(title = "Atlas", pages = 80L))
Film <- R6::R6Class("Film", public = list(title = "The garden", minutes = 12L))
#' @param item (`Book | Film`) Item to display.
item_title <- function(item) item$title
#' @return (`Book`) The book stored in the serialized bytes.
read_book <- function(bytes) unserialize(bytes)
bytes <- serialize(Book$new(), NULL)
read_book(bytes)$pages- Inside
item_title(), request completion just afteritem$. Look fortitle, which belongs to both classes. The class-specific fieldspagesandminutesare absent. Go to Type Definition onitemcan open both source classes. - On the last line, request completion just after
read_book(bytes)$. Here the return marker identifies aBook, so the list includes bothpagesandtitle.
Write the type at the start of the description for
@param, @return, or @returns. A
marker after grouped names such as @param left,right
applies to both. You can name a package class with
package::Class, include NULL in a union of up
to 32 alternatives, or use an Rd marker such as
[\code{Book}]. When code establishes a more specific return
value or reassigns a parameter, the editor follows the code.
Explicit types also help the editor choose an S3 method. In the
function below, request Signature Help inside
format(day, "%Y"). Look for the format
argument of format.Date. For a union, the server chooses a
method when the alternatives agree. S3
signatures
#' @param day (`Date`) A calendar day.
year_text <- function(day) format(day, "%Y")A parenthesized marker can also name a user-defined S3 class with no
generator, such as (`invoice`) for a generic with an
invoice method. The built-in numeric hint
allows both integer and double inputs. Use integer or
double when only one storage type is possible. A concrete
1L still selects an integer method when one exists.
Reuse parameter documentation
Add this function below item_title() and request
completion after item$. The inherited description supplies
the same Book | Film union and the shared
title field.
#' @inheritParams item_title
has_title <- function(item) nzchar(item$title)@inherit item_title params and unrestricted
@inherit item_title also inherit parameters. With a donor
documenting a, b, and c, use
@inheritParams donor b:c for a range or
@inheritParams donor -b to exclude a name. Grouped
parameters are inherited together. A local @param takes
precedence, even when it contains only prose. With several donors, the
first applicable description wins. Give a return value its own
marker.
The server looks for a unique donor in the file or indexed source
package and resolves class names in the donor’s scope. It skips
@noRd donors and shared pages assembled from multiple
roxygen blocks. A package-qualified donor identifies installed help,
which the server also skips for this lookup.
To share a description without creating a function, give a standalone
roxygen block an explicit topic name and put NULL
immediately after it. A consumer can use
@inheritParams reading-item with this donor.
#' @name reading-item
#' @param item (`Book`) A book from the catalogue.
#' @keywords internal
NULLFollow a class name through Rename
In the first example, run Find References on the
Book variable at the start of its declaration. The results
include the explicit Book markers in the parameter union
and return description. Preview Rename Symbol to
Volume to see those markers and Book$new()
change together. The literal "Book" passed to
R6Class() is the runtime class name.
The server checks which generator a marker names in its declaration’s
scope. This keeps unrelated generators and prose independent. Renaming
the function item_title also updates the verified
@inheritParams item_title reference. An explicit
@name stays the topic name when its function is renamed.
The server declines a rename when it would capture an existing topic or
invalidate an inherited parameter selection.
Describe fields, slots, and properties
Use the same leading type marker when a member’s declaration leaves
the stored type open. Add this example after the Book
declaration. Request completion just after sleeve@item$
inside sleeve_title(). The @slot marker
identifies a Book, so the editor can offer
title even though the slot is declared as
ANY.
#' @slot item (`Book`) The book stored in the sleeve.
Sleeve <- methods::setClass("Sleeve", slots = c(item = "ANY"))
#' @param sleeve (`Sleeve`) A book sleeve.
sleeve_title <- function(sleeve) sleeve@item$titleFor R6, put @field immediately above the field or active
binding. For S7, put @prop on the class for a computed
property. A class-level @param can describe a stored S7
property supplied through the generated constructor. The server can
suggest fields without calling a getter. A concrete value or a
specifically typed slot or property takes precedence.
R6 methods can document their own parameters and return values. For a
missing argument description, the server checks the class-level
@param, a matching @field for
initialize(), then the same method on a parent class. S4
registrations can document their inline method function. An S7 method
can attach a block to
method(generic, Class) <- function(...), and a return
marker on the S7 generic can supply a type when a call’s result is
otherwise unknown. Classes and
methods
Use class links for completion
Explicit markers work by default. To also use existing Markdown class
links and class descriptions, enable
r.lsp.roxygen_advisory_type_hints in the editor, or set
this option in the server’s R profile and restart it.
options(languageserver.roxygen_advisory_type_hints = TRUE)Try changing the item_title() parameter description to
@param item An object of class [`Book`].. With this option
enabled, completion after item$ offers pages.
Each linked class in a union must resolve in scope. A description of a
container, such as A list of [`Book`] objects., leaves the
element type unspecified. These advisory forms supply member
suggestions. S3 dispatch and edits to class names in annotations require
explicit markers.
Set r.lsp.roxygen_type_hints to false to
turn off inference from both explicit and advisory documentation. Member
inference from source code and ordinary S3 dispatch have their own
settings. Server
settings