Skip to content

Instantly share code, notes, and snippets.

@samesense
Last active February 17, 2026 19:05
Show Gist options
  • Select an option

  • Save samesense/a1cf9bfaa09dd9ea053e003c9c000505 to your computer and use it in GitHub Desktop.

Select an option

Save samesense/a1cf9bfaa09dd9ea053e003c9c000505 to your computer and use it in GitHub Desktop.
Demo r nvim plugin
---
title: "R.nvim Feature Demo"
format: html
---
## Getting Started with R.nvim
R.nvim is a Neovim plugin for R development. Open this file in Neovim and
follow along to explore its features.
### Starting R
Press `\rf` to start R in a split terminal. R.nvim will open an R console
beside your editor. Other start commands:
- `\rf` — Start R (default)
- `\rq` — Quit R
- `\rl` — List the R workspace (`ls()`)
- `\rc` — Clear the R console
## Executing Code
### Send a single line
Place your cursor on any line below and press `\l` to send it to R:
```{r}
x <- seq(1, 100)
y <- x^2 + rnorm(100, sd = 200)
message("x and y are now defined")
```
### Send a selection
In visual mode, select lines and press `\ss` to send the selection.
### Send an entire code block
Place your cursor inside the block below and press `\cc` to send the whole
chunk to R:
```{r}
library(datasets)
# Basic arithmetic
a <- 10
b <- 25
result <- a * b + sqrt(b)
print(paste("Result:", result))
```
### Send from the cursor to the end of the block
Press `\cd` to send from the current line to the end of the chunk.
```{r}
nums <- c(3, 7, 2, 9, 4, 6, 1, 8, 5)
sorted <- sort(nums)
print(sorted)
cat("Mean:", mean(nums), "\n")
cat("SD:", sd(nums), "\n")
```
## Plotting
R.nvim can display plots in a separate graphics window. Send the chunks below
with `\cd` to see plots rendered.
### Base R plot
```{r}
#| label: fig-scatter
#| fig-cap: "Scatter plot with trend line"
plot(x, y,
main = "Quadratic Relationship with Noise",
xlab = "x",
ylab = "y",
pch = 19,
col = rgb(0.2, 0.4, 0.8, 0.6))
abline(lm(y ~ x), col = "red", lwd = 2)
```
### ggplot2
```{r}
#| label: fig-ggplot
#| fig-cap: "Distribution of diamond prices"
library(ggplot2)
ggplot(diamonds[sample(nrow(diamonds), 2000), ], aes(x = carat, y = price)) +
geom_point(aes(color = cut), alpha = 0.6) +
geom_smooth(method = "loess", se = TRUE) +
scale_color_viridis_d() +
labs(title = "Diamond Price vs Carat",
x = "Carat",
y = "Price (USD)") +
theme_minimal()
```
### Histogram
```{r}
#| label: fig-hist
#| fig-cap: "Distribution of residuals"
residuals <- y - predict(lm(y ~ x))
hist(residuals,
breaks = 20,
col = "steelblue",
border = "white",
main = "Residuals from Linear Fit",
xlab = "Residual Value")
```
## Viewing Dataframes
R.nvim provides built-in dataframe viewing. Place your cursor on a dataframe
name and press `\rv` to view it in a tab. Navitage tabs with gt and gT.
:tabclose to close it.
### Create and inspect a dataframe
```{r}
df <- data.frame(
id = 1:20,
name = paste0("subject_", sprintf("%02d", 1:20)),
group = rep(c("control", "treatment"), each = 10),
score = c(rnorm(10, mean = 50, sd = 10),
rnorm(10, mean = 58, sd = 10)),
passed = sample(c(TRUE, FALSE), 20, replace = TRUE, prob = c(0.7, 0.3))
)
```
Now try these R.nvim object inspection shortcuts:
- `\rv` — View the dataframe (opens in a buffer via `head()`)
- `\vs` — Print `summary(df)` in the R console
- `\rh` — Show help for the object under cursor
```{r}
# Cursor on 'df' and press \rv to view it
df
# Explore structure
str(df)
summary(df)
```
### Working with built-in datasets
```{r}
# View the mtcars dataset — place cursor on mtcars and press \rv
data(mtcars)
head(mtcars)
```
```{r}
# Grouped summary
aggregate(mpg ~ cyl, data = mtcars, FUN = mean)
```
## Object Browser
Press `\ro` to open the Object Browser. It shows all objects in your R
environment organized by type. You can expand lists and dataframes to see
their structure.
```{r}
# Create some objects to see in the browser
my_list <- list(
numbers = 1:5,
text = c("hello", "world"),
nested = list(a = TRUE, b = 3.14)
)
my_matrix <- matrix(1:12, nrow = 3, ncol = 4)
my_factor <- factor(c("low", "med", "high", "low", "high"))
```
## Useful Keybinding Summary
| Keybinding | Action |
|------------|---------------------------------|
| `\rf` | Start R |
| `\rq` | Quit R |
| `\l` | Send current line |
| `\ss` | Send selection (visual mode) |
| `\cc` | Send current code block |
| `\bd` | Send block from cursor downward |
| `\rv` | View object under cursor |
| `\vs` | Summary of object |
| `\rh` | Help on object |
| `\ro` | Toggle Object Browser |
| `\rl` | List workspace |
| `\rc` | Clear console |
| `\gn` | Jump to next code chunk |
| `\gN` | Jump to previous code chunk |
| `gt` | Switch tabs |
:tabclose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment