snippet creates shareable, syntax-highlighted code images directly
from your R session. It uses Typst for rendering,
which can create png, pdf, or svg outputs.
Install the development version from GitHub:
# pak::pak('christopherkenny/snippet')
install.packages("snippet", repos = c('https://christopherkenny.r-universe.dev', 'https://cloud.r-project.org'))
snippet also requires Typst to be installed on
your system. If you have Quarto installed, it will use its bundled Typst
as a fallback. See typr for more
details on the fallback strategy.
Pass code as a string, a character vector, or a file path. Leave code
empty to use your clipboard:
library(snippet)
snippet('fit <- lm(mpg ~ wt, data = mtcars)\nsummary(fit)')

Two Flexoki themes are bundled. List
available themes with snippet_themes():
snippet(
'fit <- lm(mpg ~ wt, data = mtcars)',
theme = 'Flexoki Dark'
)

Install additional themes by name with snippet_install_theme(). See
the full list with snippet_known_themes():
snippet_install_theme('Dracula')
snippet_install_theme('Tomorrow Night')
snippet('fit <- lm(mpg ~ wt, data = mtcars)', theme = 'Dracula')
You can also install any theme from a URL or local file path:
snippet_install_theme('https://example.com/my-theme.tmTheme')
snippet_install_theme('/path/to/my-theme.tmTheme')
The package supports Windows (default) and Mac browser controls.
snippet('x <- 1', style = 'mac')

snippet(
'x <- 1:10\nmean(x)\nsd(x)',
line_numbers = TRUE,
width = 4
)