Skip to contents

Generates a new ggplot2 theme function by prompting a large language model (LLM) with a user-supplied description. The generated code is parsed, evaluated, and returned as a usable theme function.

Usage

make_ai_theme(
  chat,
  theme_prompt,
  image = NULL,
  return_type = c("function", "expression", "character"),
  additional_forbidden = NULL
)

Arguments

chat

A Chat R6 object from the ellmer package

theme_prompt

A character string describing the desired theme style.

image

An optional object of class ellmer::ContentImageInline or ellmer::ContentImageRemote, created by calling ellmer::content_image_url() or similar. The image will be added to the data that is sent to the LLM so it can be referenced in theme_prompt.

return_type

"function", "expression", or "character". Determines the type of object returned. A function is returned by default. See Value.

additional_forbidden

A character vector of additional function names to block in the generated code. This is useful for adding custom restrictions beyond the default set. The defaults can be seen by running default_dangerous_calls(). :: and ::: are always blocked, regardless of this argument.

Value

A function that can be used as a ggplot2 theme. If return_type is set to "expression" or "character", the raw code is returned in those formats instead. This allows the user inspect the code for safety before parsing/evaluating to create the final, usable function.

Details

The theme description is embedded into a prompt that is sent to the LLM. The resulting response is expected to be valid R code for a theme function. If the response cannot be parsed as a function, an error is raised.

The full conversation with the LLM remains accessible via the chat object, which may be useful for debugging or iteration.

Warning: You must set return_type to "expression" or "character" if you do not want to run LLM-generated code before review!

Examples

if (FALSE) { # \dontrun{
library(ellmer)
library(ggplot2)

theme_neon_dark <- make_ai_theme(chat,
  "dark theme with neon green accents")
 ggplot(mtcars, aes(x = hp)) + geom_histogram() +
  theme_neon_dark()
} # }