Using gander and ellmer
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
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:
- Install the
gander
andellmer
packages. - 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.
- 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.
- Also add (one of) these line to the
.Rprofile
file:
options(.gander_chat = ellmer::chat_anthropic())
options(.gander_chat = ellmer::chat_openai())
etc.
- 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 runningollama 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.
- Setup the
gander
assistant: Thegander
assistant is interfaced with the via thegander addin
. For easiest access, we recommend registering thegander 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).
- 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:
-
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 asusing-gander.R
.- In RStudio, go to
File > New File > R Script
. - Save the file as
using-gander.R
.
- In RStudio, go to
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 thedata()
function, and then use thegander
addin to generate code for a graph.When you invoke the
gander
add-in with theCTRL+Alt+G (orCtrl+Cmd+G on macOS), it will prompt you to select a dataset. You can select thepenguins
dataset from thepalmerpenguins
package, or any other dataset you have loaded in your R session.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.” Thegander
addin will then generate the R code for the graph. Hit tab to accept the generated code.You can then run the generated code in your R script to create the graph.
You can also modify the generated code to customize the graph further, such as changing the theme or adding labels.
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 thegander
addin to generate new code based on the selected code.As the code gets near to what you want to achieve, it is better ( in my experience) to switch from the
Replace
mode in thegander
addin to theAppend
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.)Finally, paste the code from the R-script into your Quarto Markdown document or blog post to share your work.