Skip to content

Instantly share code, notes, and snippets.

@jimbrig
Forked from david-diviny-nousgroup/llm-diagram
Created March 11, 2025 17:19
Show Gist options
  • Save jimbrig/23e23df465f6344a6a13bb8f86909b32 to your computer and use it in GitHub Desktop.
Save jimbrig/23e23df465f6344a6a13bb8f86909b32 to your computer and use it in GitHub Desktop.
Generate a Mermaid.js diagram using LLM with elmer package to explain code
library(elmer)
library(tidyverse)
library(DiagrammeR)
library(glue)
code <- "starwars %>%
group_by(species) %>%
summarise(
n = n(),
mass = mean(mass, na.rm = TRUE)
) %>%
filter(
n > 1,
mass > 50
)"
chat <- chat_openai(
model = "gpt-4o",
system_prompt = "You are a friendly but terse assistant.",
echo = TRUE,
api_args = list(temperature = 0)
)
response <- chat$chat(glue("Generate mermaid.js code explaining this R code:
{code}"))
mermaid_code <- str_extract(response, "(?s)(?<=```mermaid\\n).*?(?=```)")
DiagrammeR(mermaid_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment