RMarkdown Code Runner

unlisted

by Diego Sánchez Guijarro

Run R code chunks inside Markdown notes like RMarkdown (Rmd). Supports persistent R sessions per note, chunk options, inline plots/tables, persistent markdown outputs, and document knitting.

Updated 23d agoMIT
View on GitHub

RMarkdown Code Runner Logo

RMarkdown Code Runner for Obsidian

Obsidian Plugin License: MIT

An Obsidian plugin that executes R code chunks inside Markdown notes, providing an environment equivalent to RStudio, R Markdown (.Rmd), and Quarto.

The plugin provides persistent R execution sessions per note, inline output rendering (text, tables, multi-panel plots, base graphics, and ggplot2), RStudio keyboard shortcuts, contextual autocompletion with Tab support, and document knitting.


Features

Persistent R Session per Note

  • Variables, functions, loaded packages (tidyverse, ggplot2, zoo, etc.), and imported data frames persist in memory across chunks within the note.
  • The working directory (setwd()) is automatically anchored to the directory of the active Markdown note, enabling standard relative paths (such as read.csv("01_Data/file.csv")).

Output Rendering

  • Plots and Graphics: Renders high-resolution graphics from base R, ggplot2, and multi-plot arrangements (e.g. gridExtra::grid.arrange()).
  • Multi-Plot Chunks: Automatically captures and displays all sequential plots produced within a single chunk (e.g., iterative calls to acf() and pacf()) inside a scrollable output frame.
  • Structured Data Tables: Automatically formats R data.frame and knitr::kable objects into standard Markdown tables.
  • Diagnostic Badges: Clearly distinguishes errors, warnings, and console messages using dedicated semantic banners without interfering with standard output.
  • Silent Setup Chunks: Chunks configured with include=FALSE execute silently in the session without generating unnecessary empty containers in the editor, while still surfacing runtime errors if they occur.

RStudio Keyboard Shortcuts

  • Ctrl + Alt + I (Cmd + Option + I on macOS): Insert a new R code chunk.
  • Alt + - (Option + - on macOS): Insert assignment operator <-.
  • Typing <--: Automatically expands to <- inside R chunks.
  • Ctrl + Shift + M (Cmd + Shift + M on macOS): Insert native pipe operator |>.
  • Ctrl + Shift + Enter or Ctrl + Enter: Execute the active chunk.
  • Ctrl + Alt + P: Execute all chunks above the cursor position.
  • Ctrl + Alt + R: Execute all chunks in the active note.
  • Ctrl + Shift + C: Toggle line or block comments (# ).

Autocompletion

  • Suggests standard R functions (print(), paste(), ggplot(), summary(), head(), etc.).
  • Dynamically indexes variables and datasets defined in the note as you write.
  • Suggests chunk options (echo=FALSE, warning=FALSE, fig.width=, etc.) when editing chunk headers.
  • Accepts suggestions with either Tab or Enter.

Master Toggle

  • A dedicated R ribbon icon toggles R execution on and off.
  • When toggled off, all chunk toolbars, output cards, and decorators are suppressed, reverting code blocks to standard Markdown.

Document Knitting

  • Knits the active document to HTML, PDF, or Markdown via rmarkdown::render() using the command palette or shortcut (Ctrl + Shift + K).

Keyboard Shortcuts

ActionWindows / LinuxmacOSDescription
Insert ChunkCtrl + Alt + ICmd + Option + IInserts {r}\n\n at cursor
Assign OperatorAlt + - or <--Option + - or <--Inserts <-
Pipe OperatorCtrl + Shift + MCmd + Shift + MInserts |>
Run Current ChunkCtrl + Shift + Enter / Ctrl + EnterCmd + Shift + Enter / Cmd + EnterExecutes the active chunk
Run All AboveCtrl + Alt + PCmd + Option + PExecutes preceding chunks in order
Run All ChunksCtrl + Alt + RCmd + Option + RExecutes all chunks in the note
Toggle CommentCtrl + Shift + CCmd + Shift + CToggles # comment on line/selection
Knit NoteCtrl + Shift + KCmd + Shift + KKnits document to output format
Accept SuggestionTab or EnterTab or EnterAccepts active autocomplete item

Chunk Options

Both standard R Markdown chunk header syntax and Quarto #| YAML block syntax are supported.

```{r chunk_label, echo=TRUE, eval=TRUE, include=TRUE, warning=FALSE, message=FALSE, fig.width=7, fig.height=5, fig.cap="Figure Caption"}
# R code

Quarto syntax:

```r
```{r}
#| label: plot-example
#| echo: false
#| fig-width: 8
#| fig-height: 5
#| fig-cap: "Scatterplot"

library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()

| Option | Values | Default | Description |
| :--- | :--- | :--- | :--- |
| `eval` | `TRUE` / `FALSE` | `TRUE` | Whether to evaluate the chunk |
| `echo` | `TRUE` / `FALSE` | `TRUE` | Display code in knit output |
| `include` | `TRUE` / `FALSE` | `TRUE` | Include chunk output in document |
| `warning` | `TRUE` / `FALSE` | `TRUE` | Display warning messages |
| `message` | `TRUE` / `FALSE` | `TRUE` | Display console messages |
| `results` | `"markup"` / `"asis"` / `"hide"` | `"markup"` | Text output format mode |
| `fig.width` / `fig-width` | Numeric (inches) | `7` | Figure width |
| `fig.height` / `fig-height` | Numeric (inches) | `5` | Figure height |
| `fig.cap` / `fig-cap` | String | `""` | Figure caption |

---

## Output Storage Modes

Configurable under **Plugin Settings**:

1. **Dynamic UI Only (Default)**: Outputs are rendered dynamically in the editor interface without writing to the `.md` file.
2. **Persist into Markdown File**: Outputs and generated images (in `rmd-plots/`) are written directly to the file inside `<!-- rmd-output:start -->` and `<!-- rmd-output:end -->` tags.
3. **Both**: Combines interactive UI widgets with persisted Markdown blocks.

---

## Installation

### Prerequisites
- R 4.x installed locally. The plugin automatically searches default installation paths (Windows registry and standard directories, `/usr/bin/Rscript`, `/usr/local/bin/Rscript`, Homebrew), or a custom path can be specified in settings.

### Manual / Local Build
1. Clone the repository into your vault's plugins folder:
   ```bash
   cd <VaultFolder>/.obsidian/plugins/
   git clone https://github.com/diegosg-dev/CodeRunnerObsidian.git
   cd CodeRunnerObsidian
   npm install
   npm run build
  1. In Obsidian, open Settings → Community plugins and reload installed plugins.
  2. Enable RMarkdown Code Runner.

Development

# Install dependencies
npm install

# Run integration tests
npm test

# Build production bundle
npm run build

# Watch mode
npm run dev

Author

Diego Sánchez Guijarro (@diegosg-dev)


License

This project is licensed under the MIT License.

For plugin developers

Search results and similarity scores are powered by semantic analysis of your plugin's README. If your plugin isn't appearing for searches you'd expect, try updating your README to clearly describe your plugin's purpose, features, and use cases.