PyCharm 2019.3 Help

Work with R Markdown

The R plugin for PyCharm provides handy capabilities to work with the R Markdown files. With the interactive editor, you can quickly add executable chunks of R code, run and debug them, and produce the HTML output.

When you create a new *.rmd file, the following header is added to it:

--- title: "Untitled" author: < username > date: < the current date DD/MM/YYYY > output: rmarkdown::html_vignette ---

This header is used to render output in an HTML file. To modify the default header, edit the R Markdown template in the project Settings/Preferences.

In the R Markdown file you can add any context according to the Markdown syntax and some executable code chunks.

Add a code chunk

  1. To add a new R chunk to your R Markdown file, position the caret at any line or the code chunk, then click Add the code chunk or press Ctrl+Alt+Insert.

    Adding a new code chunk to a R Markdown file
    The following construct will be added below the selected line or chunk:
    ```{r} ```

  2. Type any R code in the chunk.

    When adding R code to the chunk, use the code assistance features, such as code completion, code inspections, and quick fixes.

    Code completion for R

Once the code is added the execution actions become available through the Run menu in the gutter and in the chunk toolbar.

Executable code chunk in the editor

Item

Description

Run the chunk

Executes the current code chunk.

Debug the chunk

Debugs the current code chunk.

Run chunks above

Executes all the code chunks above the current chunk.

Run chunks below

Executes all the code chunk below the current chunk.

Execute code chunks

  1. To execute a code chunk at the caret, click Run Chunk and select Run Chunk, or click Run Chunk on the chunk toolbar.

  2. When executing one chunk at a time, mind code dependencies. For example, the second chunk in the code fragment uses the variable defined in the first chunk.

    code dependency

    Click Execute chunks above to execute all chunks above the currently selected chunk to make sure that all required variables are initialized.

  3. At any time you can click Execute all chunks below to execute all remaining code chunks in the file, or opt to the execution all code chunks at once by clicking Run all.

  4. Preview and evaluate the results of the execution that are rendered below the chunk.

    Results of the chunk execution

    You can also switch to the R Console to study variables in-detail.

    Preview variables in the R Console
  5. If needed, click Clear output to clear the execution results.

Debug code chunks

You can debug executable chunks in R Markdown files to detect and fix any errors in them.

  1. Click the gutter to create breakpoints.

    Before debugging a particular code chunk, ensure that you have debugged all chunks with its code dependencies. Similar to running code chunks, you cannot perform debugging, if any of the variables used in the current chunk have not been initialized.

  2. Click Start debugger on the chunk toolbar.

    Switch to the R Console. The debugging process stops at a breakpoint and you can preview the current results in the Variables window.

    Debugging code chunks in an R Markdown file in PyCharm
  3. Manage the debugging process with the debugging toolbar.

You can create an HTML file that includes both the R Markdown source code and the results of its execution.

Generate the HTML output

  1. Click Render output on the R Markdown toolbar.

  2. PyCharm creates an HTML file with the same name as the .rmd file. The R Markdown console reports on the task completion.

    Rendering an HTML file

    By default, all output files are stored in the project root directory. If needed, select Custom from the list of the output directories on the R Markdown toolbar and specify an alternative location for the output files.

  3. Click Open an output file in a browser to open the output file in the browser.

    An HTML output file in a browser
Last modified: 14 January 2020