Shiny Integration

aisdk provides seamless integration with Shiny, allowing you to build AI-powered applications where the AI can control the UI and interact with reactive input/output.

The AI Chat UI

Use aiChatUI() and aiChatServer() to add a chat interface.

library(shiny)
library(aisdk)

ui <- fluidPage(
  aiChatUI("chat")
)

server <- function(input, output, session) {
  aiChatServer("chat", model = "openai:gpt-4o")
}

shinyApp(ui, server)

AI-Controlled Apps (The Copilot Pattern)

You can give the AI tools to modify reactiveValues, effectively letting it control your app.

1. Define Reactive Values

rv <- reactiveValues(
  plot_color = "blue",
  title = "My Plot"
)

2. Create Reactive Tools

Use reactive_tool() to define tools that can read/write these values.

change_color <- reactive_tool(
  name = "change_color",
  description = "Change the plot color",
  parameters = z_object(color = z_string()),
  execute = function(rv, session, color) {
    rv$plot_color <- color
    paste("Changed color to", color)
  }
)

3. Initialize Server with Tools

server <- function(input, output, session) {
  # ... rv definition ...
  
  # Wrap tools with current session context
  tools <- wrap_reactive_tools(list(change_color), rv, session)
  
  # Provide context so AI "sees" the current state
  get_context <- reactive({
    list(current_color = rv$plot_color)
  })

  aiChatServer(
    "chat",
    model = "openai:gpt-4o",
    tools = tools,
    context = get_context
  )
}

Now the user can say “Change the plot to red”, and the AI will execute the tool, updating the Shiny app instantly.

RMarkdown and Quarto Integration

aisdk also includes a custom knitr engine ai for AI-assisted code generation. In .Rmd documents you use direct ai chunks; in .qmd documents you should use {r} chunks with #| engine: ai.

1. Register the AI Engine

Before using ai chunks in your document, register the engine in an initial setup chunk.

2. Basic Usage

If you do not opt into review mode, ai keeps the simpler legacy flow: prompt the model, extract code, and execute immediately when eval = TRUE.


::: {.cell}

```{.ai .cell-code}
Create a scatter plot of mpg vs wt from the mtcars dataset, 
colored by the number of cylinders (cyl). Make it look nice using ggplot2.
```
:::

For Quarto, the equivalent pattern is:


::: {.cell}

```{.ai .cell-code}
Create a scatter plot of mpg vs wt from the mtcars dataset,
colored by the number of cylinders (cyl). Make it look nice using ggplot2.
```
:::

3. Session and Provider Options

Provider/session options for ai chunks:

  • model: Specific model identifier. Defaults to get_model(), which resolves the package-wide default model configured with set_model().
  • session: Named model conversation shared across ai chunks. Defaults to "default".
  • new_session: If TRUE, resets the named session before the chunk runs.
  • system: Additional system prompt instructions.
  • context: Controls how R objects are sent to the model. Use FALSE to disable, or a character vector to specify exact objects.

Context Auto-Detection

If your prompt mentions variables that already exist in the knitr environment, the engine can automatically summarize those objects and pass the summaries to the model. This keeps the prompt grounded in the current document state.

For example, if you load a dataset in a previous {r} chunk:


::: {.cell}

```{.r .cell-code}
my_data <- data.frame(category = c("A", "A", "B", "C"), value = rnorm(4))
```
:::



::: {.cell}

```{.ai .cell-code}
Calculate the mean of 'value' grouped by 'category' in my_data using dplyr.
```
:::

The engine sees my_data in the prompt, finds it in the knitr environment, and sends its schema to the model so it can write accurate code.

You can manually control this with the context chunk option: - context = "my_data" explicitly includes only my_data - context = c("data1", "data2") includes multiple specific objects - context = FALSE disables context passing entirely

4. Quarto Notes

Quarto .qmd files should use {r} chunks with #| engine: ai. Direct ```{ai ...} blocks in Quarto are not being executed by Quarto here; they are rendered literally instead of being dispatched to the registered knitr engine.

5. Behavior Notes

The ai engine in R Markdown and Quarto is intentionally simple:

  • it sends the prompt to the model
  • extracts executable R code from fenced code blocks
  • executes that code immediately when eval = TRUE
  • returns the model text directly when eval = FALSE

If you need human review or workflow state, that should live outside the static document rendering path.