DataSpell 2023.2 Help

Work with R Markdown

The R plugin for DataSpell 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.

The plugin supports different markdown content: documents, notebooks, presentations, and Shiny (interactive markdown).

When you create a new *.rmd file, you can select the file type:

Create a new Markdown file

Each file is create with a specific header that depends on its type:

--- 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.

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, place the caret at any line or the code chunk, then click Add the code chunk or press Control+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.

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

Debug code chunks

  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. DataSpell 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

You can present your R markdown content in a form of presentation.

Build a presentation

  1. Create a new *.rmd file and select Presentation when specifying its type.

  2. Use ## headers to mark each new slide. The following code sample creates three content slides:

    ## Bar plot ```{r} mycars <- within(mtcars, {cyl <- ordered(cyl)}) ``` ```{r} cyls <- table(mycars$cyl) barplot(cyls, main="Car cylinder distribution", col = "#d4724e") ``` ## Pie ```{r} slices <- c(10, 12,4, 16, 10) lbls <- c("US", "UK", "Australia", "Germany", "France") mytable <- (slices) pie(slices, labels = lbls, main="Pie Chart of Countries") ``` ## Dot plot ```{r} dotchart(mtcars$drat,labels=row.names(mtcars),cex=.7, main="Rear axle ratio") ```
  3. Click Open an output file in a browser to open the generated presentation in the browser.

    R Markdown presentation

You can also build R Markdown with interactive content by using Shiny.

Create interactive widgets with Shiny

  1. Create a new *.rmd file and select Shiny when specifying its type.

  2. Edit the code chunk in the template file or click Add a new chunk to create a new code chunk.

  3. Add your Shiny code, for example, one sample from the Shiny Gallery:

    ```{r echo = FALSE} # Define UI for app that draws a histogram ---- ui <- fluidPage( # App title ---- titlePanel("Hello Shiny!"), # Sidebar layout with input and output definitions ---- sidebarLayout( # Sidebar panel for inputs ---- sidebarPanel( # Input: Slider for the number of bins ---- sliderInput(inputId = "bins", label = "Number of bins:", min = 1, max = 50, value = 30) ), # Main panel for displaying outputs ---- mainPanel( # Output: Histogram ---- plotOutput(outputId = "distPlot") ) ) ) # Define server logic required to draw a histogram ---- server <- function(input, output) { # Histogram of the Old Faithful Geyser Data ---- # with requested number of bins # This expression that generates a histogram is wrapped in a call # to renderPlot to indicate that: # # 1. It is "reactive" and therefore should be automatically # re-executed when inputs (input$bins) change # 2. Its output type is a plot output$distPlot <- renderPlot({ x <- faithful$waiting bins <- seq(min(x), max(x), length.out = input$bins + 1) hist(x, breaks = bins, col = "#75AADB", border = "white", xlab = "Waiting time to next eruption (in mins)", main = "Histogram of waiting times") }) } shinyApp(ui, server) ```
  4. If any code line is highlighted, hover it over: you might have not the shiny package installed. Press Alt+Enter and install the missing package.

  5. Click Run to build the output.

    Interactive widgets in R Markdown with Shiny
Last modified: 01 September 2023