ai in r

an experiment

Published

September 25, 2025

We’re still figuring out how ai, and LLM’s in particular, will affect data science. You might be asked in an interview to show how you might use it in some novel way. Here’s something you can talk about.

all of this motivated by this video

hadley wickam does ai n r

1. get a gemini api key

  1. google: get a gemini api key
  2. learn how to add your key to your R environment.
  3. get your key here
  4. in the upper right corner click “Create API key”

2. create an r-script.

Enter the line below and paste in your api key

3. paste your key below and execute

my_gemini_api_key = "XXXXXXXXXXXXX9FRPvvB85H_7YA0VqK_mlyiU94"

4. install ellmer

ellmer: an R - LLM package

 install.packages("ellmer")

then

5. install usethis

this makes it easy to change settings

install.packages("usethis")
library(usethis)

then

6. set your google-gemini api key

do this, which opens a file for you to edit in the Editor:

usethis::edit_r_environ()

Now you should look to the Editor and add this line

# in the .renviron file do
GEMINI_API_KEY=blah-blah-blah-whatever

then

7. restart R (click session)

then

8. load ellmer

library(ellmer)

9. then: choose a provider

Use gemini unless you know what you’re doing. It’s free. Learn more here.

chat = chat(name="google_gemini")

10. use it like this

chat$chat("what does the r-function apropos do?")

# I forgto what command I wanted, but I know it had "app" in it
apropos("app")

11. you can use it like you do in the browser

chat$chat("extract the name and age of each sentence I give you")

chat$chat("david, 17, felt like it was day 21")

chat$chat("jaime is turning seventeen this coming September.")

12. it can extract numbers from strings and help with code

chat$chat("separate this into month,day and year: july12_1938 and show me how to this using tidyverse")


# another example
chat$chat("how do extract the numbers from th list")

13. programming tools

type_person <- type_object(
  color = type_string(),
  age  = type_number()
)

prompts <- list("red is 42 now", "green is 17",  
                "blue, 25, climbs inside", "yellow will soon hit the big 5-0")

chat$chat_structured(prompts[[1]], type = type_person)

14. one last query

chat$chat("what is an interesting statistic about the palmerpenguins data")

# can you make a plot to display it?