PyCharm 2019.3 Help

Setup an R project

Before you start

  1. Ensure that you have downloaded and installed R.

  2. Install the R plugin as described in Managing plugins.

Whatever you do with your R scripts in PyCharm, you do it the context of a project. So, to start coding in R, you need to create an R project and setup a working environment.

Create an R project

  1. Do one of the following:

    • From the main menu, choose File | New Project

    • On the Welcome screen, click Create New Project

    The New Project dialog opens.

  2. In the New Project window, select the R Project type from the list of the available project types. Then specify the project name and its location.

  3. You should also create a project interpreter that allows you to work with the specific set of R packages. Expand the Project interpreter area and click New project interpreter next to the Base interpreter field. In the file system, select the R executable file. A sample path to the R executable can be /usr/local/bin/R on macOS or C:\Program Files\R\R-3.6.1\bin\R.exe on Windows.

    Adding a new project R interpreter

    Click Create to complete the task.

To create another R interpreter for your project, press Ctrl+Alt+S, go to the Settings/Preferences | Project | Project R Interpreter dialog, click Add an R interpreter, select Add, and specify a new R executable for the project interpreter.

Once you've created a new R project, PyCharm generates the main.R file with some project-related information based on the R file template.

Autogenerated .R file

At this point, you are all set to start coding in R, still you might want to modify some project settings to tailor them for your needs.

Manage R packages

  1. To open the R Packages tool window, do one of the following:

    • Select the R Packages tab in the Data View tool window group.

    • From the main menu select View | Tool Windows | R Packages

    The list of the installed R packages
  2. Click Install a package to add a new package. In the Available Packages dialog, type the name of the target package in the Search field, then select a package, and click Install.

  3. If needed, you can update packages to a newer versions. Click Refresh the package list to preview the latest available versions of the installed packages, then upgrade either a particular package (Upgrade the package) or all the packages (Upgrade all packages).

    When upgrading all packages to the available latest versions, PyCharm shows you the list of the packages to upgrade and asks to confirm the action. Click OK to proceed with the update.

    The list of the packages to upgrade

Configure R templates

  1. File templates specify default contents to be generated when creating a new file. To establish the unified style and initial code for all .R files, press Ctrl+Alt+S to open project Settings/Preferences, go to Editor | File and Code Templates, select R Script in the list of languages, and alter the default template. In the following example, we modified the default settings and added the ${TIME} variable to include the time when the file was created.

    File template settings for R
  2. Similarly, setup a file template for R Markdown files. Refer to File and code templates for more details.

  3. By using live templates, you can insert frequently-used constructions into your code, for example, loops, conditions, various declarations, or print statements. To preview the R constructs, press Ctrl+Alt+S to open project Settings/Preferences, go to Editor | Live Templates, select R Script in the list of languages

    Live template settings for R
Last modified: 14 January 2020