PyCharm 2020.3 Help

Build R packages

With the R plugin for PyCharm, you can build your own R packages. The plugin provides you with the dedicated project type and handy UI tools to build, check, and test your sources.

  1. To create a project, do one of the following:

    • From the main menu, choose File | New Project

    • On the Welcome screen, click Create New Project

  2. In the New Project dialog, select R Package from the available project types.

    Create a new R package
  3. Specify the following parameters:

    • Location: the project directory name. This name is used to create a package title and construct filenames for all sources in the package.

    • Python interpreter. Expand the Python 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.

    • Chosen package manager: the primary tools for building R packages in the project. Expand More settings and select a package manager from the list: Packrat, Rcpp, RcppArmadillo, RcppEigen, or Devtools. If you leave this setting unchanged, the default package managed will be used.

    Click OK to complete the task.

  4. Expand the project content root in the Project tool window. The plugin has created all the required directories and template files.

    Default structure of an R package

  5. Modify the source files and add all the required details according to the Writing R Extensions recommendations.

    • man: the directory that should contain documentation files for the objects in the package in R documentation (*.Rd) format.

    • R: the directory that contains only R code files.

    • DESCRIPTION: the file that contains basic information about the package in the specific format.

    • NAMESPACE: the file that can contain one or more useDynLib directives which allows shared objects that need to be loaded.

    • Read-and-delete-me: the package build instruction.

    Optionally, you can create the src directory if you have any C, C++, or Fortran sources.

  6. When all source files are added, you're ready to install and reload your package. In the Build tab of the R Tools window, click Install and reload.

    Building a package

    Once this done, the tarball file is create and the package is available for your own use.

  7. To check if your package passes the R CMD check and can be uploaded to cran, click Check the package.

  8. You can tune the build options. Click Build options and deselect or keep selected the following checkboxes:

    Build options

    Each options is selected by default and represents a particular command-line option.

If case of any reported errors or warnings during a package build, click Test the package and test the package.

Test a package

  1. Create a test for an R file by one of the following ways:

    Click An R test icon on the R file toolbar.

    Create a test with an R toolbar

    Select a target R file in the Project tool window, right-click it, and from the context menu, select Create Test File.

    Create a test file in the context menu

    A newly added test file will be placed under the test directory in the project root. You can use the same editor icon and context menu to navigate from an R file to its test.

  2. Add some testing code to the newly create test file.

  3. You can run each test individually by calling the Run action in a target test file (the Run a test icon on the R file toolbar), or you can run all tests for the package using the corresponding icon in the Build tab of the R Tools window.

    Running test

  4. Preview the test run results and fix the R code when appropriate.

Last modified: 08 March 2021