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:
Each file is create with a specific header that depends on its type:
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
To add a new R chunk to your R Markdown file, place the caret at any line or the code chunk, then click or press Ctrl+Alt+Insert.
The following construct will be added below the selected line or chunk:
```{r} ```Type any R code in the chunk.
When adding R code to the chunk, use the coding assistance features, such as code completion, code inspections, and quick fixes.
Once the code is added the execution actions become available through the Run menu in the gutter and in the chunk toolbar.
Item | Description |
---|---|
Executes the current code chunk. | |
Debugs the current code chunk. | |
Executes all the code chunks above the current chunk. | |
Executes all the code chunk below the current chunk. |
Execute code chunks
To execute a code chunk at the caret, click and select Run Chunk, or click on the chunk toolbar.
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.
Click to execute all chunks above the currently selected chunk to make sure that all required variables are initialized.
At any time you can click to execute all remaining code chunks in the file, or opt to the execution all code chunks at once by clicking .
Preview and evaluate the results of the execution that are rendered below the chunk.
You can also switch to the R Console to study variables in-detail.
If needed, click 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
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.
Click 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.
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
Click on the R Markdown toolbar.
DataSpell creates an HTML file with the same name as the .rmd file. The R Markdown console reports on the task completion.
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.
Click to open the output file in the browser.
You can present your R markdown content in a form of presentation.
Build a presentation
Create a new *.rmd file and select Presentation when specifying its type.
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") ```Click to open the generated presentation in the browser.
You can also build R Markdown with interactive content by using Shiny.
Create interactive widgets with Shiny
Create a new *.rmd file and select Shiny when specifying its type.
Edit the code chunk in the template file or click to create a new code chunk.
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) ```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.Click to build the output.