R packages needed:

install.packages(c("ellmer", "tidyverse", "usethis", "glue"))

Get a Google Gemini API key:

https://ai.google.dev/gemini-api/docs/api-key

Add the API key to your R environment. First, run this:

usethis::edit_r_environ()

Then, in the file that comes up, copy this and add your API key:

GOOGLE_API_KEY = "YOUR API KEY HERE"

Save the file and restart R Studio.

To test the setup, run this:

library(ellmer)
library(tidyverse)
library(glue)

chat <- chat_gemini()

chat$chat("Tell me three jokes about statisticians.")

If you get back three jokes, it’s all set up and ready to roll.