Code Diagnostics in the RStudio IDE


The RStudio IDE gained the ability to perform static and dynamic analysis of R code with v0.99. This article outlines the features available in the IDE.

Enabling Diagnostics

Diagnostics can be enabled and options can be set within the Global Options -> Code -> Diagnostics editing pane:

A brief outline of the options available:

Show Diagnostics for R

Toggle the display of R code diagnostics.

Enable diagnostics within R function calls

Controls whether diagnostics are performed within function calls, e.g. dplyr::select(mtcars, mpg, cyl). Toggle this if your code makes heavy use of non-standard evaluation, and RStudio is unable to produce correct diagnostics for you.

Check arguments to R function calls

Try to detect whether a particular call to a function will succeed. The diagnostics engine will report if it detects missing arguments, unmatched arguments, partially matched arguments, and too many arguments.

For example, in the code sample below, RStudio detects that the function add_numbers is missing the y argument. Note that it is not necessary for add_numbers to exist in the current environment (ie, within the running R session)

Similarly, missing arguments (alongside extra, or missing, commas) are reported:

Warn if variable used has no definition in scope

Warn if a symbol is used with no definition in the current, or parent, scope. The diagnostics engine will supply a suggestion if there appears to be a typo in the symbol’s name; that is, if a symbol with a similar name exists in scope as well.


Warn if variable is defined but not used

This diagnostic helps to identify is a variable is created but never used. This can be helpful when attempting to clean up old code, or in diagnosing other errors (wherein you believe a particular variable should be used, but isn’t).

In the following example, the variable result is assigned but never used or returned; instead, the sum is re-computed and then returned. 

Provide R style diagnostics (e.g. whitespace)

The style diagnostic checks to see if your code conforms to Hadley Wickham’s style guide, and reports style warnings when encountered. In particular, the diagnostics engine attempts to identify inappropriate use (or lack thereof) of whitespace.

Currently, the style diagnostics feature is not user configurable; this may be addressed in an upcoming release.

Other Diagnostics

Diagnostics for other languages, such as C / C++, JavaScript, and Python, are also available.

The C / C++ diagnostics engine is powered by libclang, and is able to report compiler errors and warnings inline in the source document:

Viewing Diagnostics

When an error or warning is discovered, it is reported in two places:

  1. The left gutter will show an icon related to the diagnostic's severity, and
  2. The associated diagnostic will be underlined.

Show Diagnostics

These features allow you to control when diagnostics are presented and updated. Diagnostics can either be shown on save, or after a certain amount of keyboard idle time.

Project-Level Diagnostics

You can run the diagnostics engine over all R files in the project either using the code wizard menu, or by pressing CMD + ALT + SHIFT + P (CTRL on Windows):

After running diagnostics over the project (this may take some time), you will see the Markers pane populated with all discovered warnings and errors.

Magic Comments

RStudio’s diagnostics engine can be controlled on a per-file basis by adding magic comments to the document.

The following settings are currently supported:

    # !diagnostics off -- disable diagnostics within this document.

    # !diagnostics style=[true/false] -- toggle style diagnostics for this document.

    # !diagnostics level=[syntax/core/all] -- toggle the level, or severity, of diagnostics reported.

In addition, diagnostic warnings for particular variables can be suppressed with the suppress keyword: 

    # !diagnostics suppress=<comma-separated list of variables>

For example, in the below code snippet, the usage of global_variable is not reported, while the usage of other_global_variable is:

Bug Reporting

R is a very dynamic language, and so statically producing correct diagnostics is a very difficult task. If you discover any bugs or errors in the diagnostics system, please let us know at