PyCharm 2020.2 Help

Edit R files

When you have created and configured an R project in PyCharm, you can start editing the default main.R file. However, you can add more .R and .rmd files to your project.

Add new R and R Markdown files to the project

  1. Do one of the following:

    • Right-click the target directory in the Project tool window, and select New from the context menu.

    • Press Alt+Insert

  2. Select R Script or RMarkdown file.

  3. In the dialog that opens, type a filename. For a RMarkdown file specify its type: document, notebook, presentation, or Shiny (interactive markdown).

A new file has the .R (R file icon) or .rmd extension (RMarkdown file icon).

PyCharm enables code assistance to facilitate editing and validating R code.

Code assistance in R plugin

  • Coding style. Open the code style settings to the preview the default options and modify them according to your preferences. Press Ctrl+Alt+S and go to Editor | Code Style | R, then inspect the settings:

    R code style settings

    Modify any settings if needed. For example, alter the alignment of the assignment operators or comments:

    Changing alignment settings for R code
  • Code completion helps you complete the names of classes, functions, and variables. Start typing the name of the code construct, and the suggestion list appears.

    Code completion for R
    Code completion for R

    By default, PyCharm provides case-insensitive code completion. To disable it or alter its behavior, clear the Match case checkbox in the project settings (Settings/Preferences | Editor | General | Code Completion).

    Match case for completion

  • Code inspections detect and correct various problems in your R script.

    R inspection: unresolved reference

    As soon as the IDE finds a way to improve your code, it displays a yellow bulb icon yellow bulb icon in the editor next to the current line. By clicking this icon, you can view intention actions available in the current context. Intention actions cover a wide range of situations from warnings to optimization suggestions.

    You can view the full list of available intention actions, and disable actions that you do not need. In the Settings/Preferences dialog Ctrl+Alt+S, go to Editor | Intentions, and search for the R language.

  • Quick fixes allow you to resolve a detected problem in one click. A red bulb with an exclamation mark the Quick-fix icon indicates the problematic code and prompts you to select an associated fix. Click the bulb icon or press Alt+Enter to open the list of the fixes.

    Quick-fix fix for the missing import statement

    With the quick fixes for missing import statements you don't need to manually add every single R package to your project. You can just select the Install all missing packages in file option and PyCharm automatically installs all the packages required for your R script.

  • Hinting allows you to quickly grasp the structure of the code construct and required types of parameters. Place the caret at any function and press Ctrl+P to preview information about its parameters.

    Show function parameters

    By default, PyCharm shows the inlay hints for the R language. You can right-click any inlay element and configure this behavior.

    Context menu to configure inlay hints for R

    You can disable hints or specify more configuration options in the project Settings/Preferences | Editor | Inlay Hints | R.

    You can restrict or limit the parameter hints by adding some construct patterns to the black list. Click the Black list link and type one of the shown patterns in the field. Then click OK to save the changes.

    Adding inlay hints constructs to the black list

    In some R functions, the return statement can be omitted. Still, you can configure the function syntax to show the implicit NULL result. You can also specify the minimal number of result statements to show the return value hints.

    Configuring inlay hints for the return values

  • Auto saving

    PyCharm automatically saves changes that you make in your files. Saving is triggered by various events, such as compiling, running, debugging, performing version control operations, closing a file or a project, or quitting the IDE. Most of the actual events are predefined and cannot be configured, but you can be sure that changes will not be lost as you work.

  • Quick documentation

    With the R plugin you can quickly preview reference documentation for a particular variable, class, type, or argument. In the R console, type ? and the code element name, for example, ?barplot. The corresponding reference documentation appears in the Documentation tab of the R Tools window.

    Viewing quick documentation for the barplot class

    To preview documentation for a particular package, click its name in the Package column of the Packages tab of the R Tools window.

    R package documentation

  • When editing an R file, you can quickly navigate through your code and jump to a particular variable or function. You can open the Structure tool window or select the target variable in the editor and click the corresponding breadcrumb element to open the structure popup window.

    View the R file structure

Last modified: 19 August 2020