The Mad Hatter’s Guide to Data Viz and Stats in R
  1. Data Viz and Stats
  2. AI Tools
  3. Using gander and ellmer
  • Data Viz and Stats
    • Tools
      • Introduction to R and RStudio
    • Descriptive Analytics
      • Data
      • Inspect Data
      • Graphs
      • Summaries
      • Counts
      • Quantities
      • Groups
      • Distributions
      • Groups and Distributions
      • Change
      • Proportions
      • Parts of a Whole
      • Evolution and Flow
      • Ratings and Rankings
      • Surveys
      • Time
      • Space
      • Networks
      • Miscellaneous Graphing Tools, and References
    • Inference
      • Basics of Statistical Inference
      • 🎲 Samples, Populations, Statistics and Inference
      • Basics of Randomization Tests
      • Inference for a Single Mean
      • Inference for Two Independent Means
      • Inference for Comparing Two Paired Means
      • Comparing Multiple Means with ANOVA
      • Inference for Correlation
      • Testing a Single Proportion
      • Inference Test for Two Proportions
    • Modelling
      • Modelling with Linear Regression
      • Modelling with Logistic Regression
      • 🕔 Modelling and Predicting Time Series
    • Workflow
      • Facing the Abyss
      • I Publish, therefore I Am
      • Data Carpentry
    • Arts
      • Colours
      • Fonts in ggplot
      • Annotating Plots: Text, Labels, and Boxes
      • Annotations: Drawing Attention to Parts of the Graph
      • Highlighting parts of the Chart
      • Changing Scales on Charts
      • Assembling a Collage of Plots
      • Making Diagrams in R
    • AI Tools
      • Using gander and ellmer
      • Using Github Copilot and other AI tools to generate R code
      • Using LLMs to Explain Stat models
    • Case Studies
      • Demo:Product Packaging and Elderly People
      • Ikea Furniture
      • Movie Profits
      • Gender at the Work Place
      • Heptathlon
      • School Scores
      • Children's Games
      • Valentine’s Day Spending
      • Women Live Longer?
      • Hearing Loss in Children
      • California Transit Payments
      • Seaweed Nutrients
      • Coffee Flavours
      • Legionnaire’s Disease in the USA
      • Antarctic Sea ice
      • William Farr's Observations on Cholera in London
    • Projects
      • Project: Basics of EDA #1
      • Project: Basics of EDA #2
      • Experiments

On this page

  • 1 Introduction
  • 2 Setting Up R Packages
  • 3 Setting up gander and ellmer
  • 4 Using gander
  • 5 References
  1. Data Viz and Stats
  2. AI Tools
  3. Using gander and ellmer

Using gander and ellmer

news
code
analysis
Author

Arvind V.

Published

June 14, 2025

Modified

September 22, 2025

1 Introduction

I am going to use gander to create a simple example of how to use it. It seems gander can allow the user to see the data they are working with and is able to generate accurate R code for graphs.

2 Setting Up R Packages

library(tidyverse)
library(ggformula)
library(gander)
library(ellmer)

3 Setting up gander and ellmer

There are a few steps involved in setting up gander and ellmer to work with your preferred LLM (Large Language Model) service. The steps are as follows:

  1. Install the gander and ellmer packages.
  2. Get your API Key, from say Anthropic, ChatGPT, or Ollama.
  • Anthropic: visit https://console.anthropic.com/settings/keys
  • ChatGPT: visit https://platform.openai.com/api-keys
  • Ollama: visit https://ollama.com/ # runs LLMs LOCALLY! NO API Key needed.

See “Choosing a model” in vignette("gander", package = "gander") to learn more.

  1. Save the API_KEY of you preferred LLM service in your .Rprofile file. For example, if you are using Ollama, you would add the following line to your .Rprofile, as follows:
## RUN THESE IN YOUR CONSOLE
install.packages("usethis")
library(usethis)
usethis::edit_r_profile()

This will open the .Rprofile file in your RStudio editor. You will need to add (one of ) the following lines to the file:

OPENAI_API_KEY = “YOUR_API_KEY_HERE”

ANTHROPIC_API_KEY = “YOUR_API_KEY_HERE”

etc.

  1. Also add (one of) these line to the .Rprofile file:

options(.gander_chat = ellmer::chat_anthropic())

options(.gander_chat = ellmer::chat_openai())

etc.

  1. If you wish to run models locally for free, you can use Ollama. In that case, you will need to:
  • install the Ollama CLI https://ollama.com/download

  • download a model from https://ollama.com/models, say “llama3.1”

  • then add the following line to your .Rprofile file: options(.gander_chat = ellmer::chat_ollama(model = "llama3.1"))

  • MacOS: Ensure you start ollama server by running ollama serve in your terminal.

  • Windows: Add ollama.exe to your startup programs (see https://ollama.com/docs/installation/windows for details).

    Note: If you are using Ollama, you do not need an API key, but you do need to have the Ollama server running.

  1. Setup the gander assistant: The gander assistant is interfaced with the via the gander addin. For easiest access, we recommend registering the gander addin to a keyboard shortcut.

In RStudio, navigate to Tools > Modify Keyboard Shortcuts > Search "gander". The package author suggests Ctrl+Alt+G (or Ctrl+Cmd+G on macOS).

  1. Restart RStudio to ensure the changes take effect.

4 Using gander

Now that we have set up gander and ellmer, we can use the gander/ellmer packages to get working with creating code with AI tools. Let us follow these steps:

  1. It seems ( for now, June 2025) that gander is best used in an *R-script ( a .R file). So let use create a new R script file in RStudio, and save it as using-gander.R.

    • In RStudio, go to File > New File > R Script.
    • Save the file as using-gander.R.
  2. In the R script, we can use the gander addin to generate code for us. The addin will prompt us to select a dataset and then ask us to describe the graph we want to create. Load up a dataset using the data() function, and then use the gander addin to generate code for a graph.

  3. When you invoke the gander add-in with the CTRL+Alt+G (or Ctrl+Cmd+G on macOS), it will prompt you to select a dataset. You can select the penguins dataset from the palmerpenguins package, or any other dataset you have loaded in your R session.

  4. After selecting the dataset, the gander addin will ask you to describe the graph you want to create. You can type a description like “Create a scatter plot of bill length vs body mass, colored by species, with a linear model fit.” The gander addin will then generate the R code for the graph. Hit tab to accept the generated code.

  5. You can then run the generated code in your R script to create the graph.

  6. You can also modify the generated code to customize the graph further, such as changing the theme or adding labels.

  7. You can use the gander addin again to generate code for another graph, or to modify the existing graph. Select the existing code each time and invoke the gander addin to generate new code based on the selected code.

  8. As the code gets near to what you want to achieve, it is better ( in my experience) to switch from the Replace mode in the gander addin to the Append mode. This way, you can keep adding to the existing code without losing the previous code. ( It also helps when the tool occasionally changes existing code, or even hallucinates code that does not work.)

  9. Finally, paste the code from the R-script into your Quarto Markdown document or blog post to share your work.

5 References

  1. https://posit.co/blog/introducing-gander/
  2. https://simonpcouch.github.io/gander/
  3. https://www.howardbaik.com/posts/deep-dive-into-ellmer-part1/
  4. Doing Data Analysis with AI. https://gabors-data-analysis.com/ai-course/
Back to top
AI Tools
Using Github Copilot and other AI tools to generate R code

License: CC BY-SA 2.0

Website made with ❤️ and Quarto, by Arvind V.

Hosted by Netlify .