3.2 languageserver package

The R language server implements the Language Server Protocol (LSP) and provides a set of language analysis features such as completion, providing function signatures, extended function documentation, locating function implementations, occurrences of a symbol or object, rename symbol, and code diagnostics and formatting. The R language server statically analyzes R code, and vscode-R interfaces with it to provide the core of this extension’s functionality.

The R language server is implemented by the languageserver package which performs static code analysis with the latest user documents in R and Rmd languages. Therefore, it does not rely on an active R session and thus does not require the code to be executed.

vscode-R settings

3.2.1 Highlight Features:

  • styler

    The language server provides code formatting through the through the styler package in R. See here for configuration.

    Main usage: select the code block you want to format, right-click and select Format Selection. Note that this only works in the Edit Mode when Vim is enabled.

    Alternatively, right-click at anywhere in the code editor and select Format Document to format the entire document.

  • Rename symbols

    Place the cursor on the symbol you want to rename, right-click and select Rename Symbol. A dialog will pop up, allowing you to enter the new name for the symbol. A refactoring preview will be shown, allowing you to review the changes before applying them.

  • Find References

    Right-click on an R object,

    • select Find All References to find all references to the object in the current workspace.

      The results will be shown in the References view in the Activity Bar on the left side of the window.

    • select Go To References open a popup showing all of the uses of the object within the current document.


3.2.2 lintr

R code linting (diagnostics) is provided by lintr via language server and is enabled by default. It provides syntax error warnings as well as style guidelines.

Configuration

To configure the behavior of lintr, you should create/edit the global lintr config file at ~/.lintr. Alternatively, you can also create/edit a project-specific lintr config file at ${workspaceFolder}/.lintr.

  • Do not forget the new empty line at the end of the file.

    To be sure that the file is correctly set up you can use:

    read.dcf(".lintr") # Should give no error

    If the file is not available in the workspace you can add it with:

    options(lintr.linter_file="Path/to/file/.lintr")

    You can also add this line in your .Rprofile to not have to run it everytime. ref ↩︎

  • Visit Individual linters for a complete list of supported linters.

  • Visit the Configuring linters for a complete guide to customizing lintr config regarding the list of linters and file or line exclusions.

Q: How to disable lintr?
A: Set "r.lsp.diagnostics": false. Then in command palette, type “Developer: Reload Window” for the changes to take effect.