---
title: "ai in r"
subtitle: "an experiment"
date: today
toc: true
format:
  html: 
    code-overflow: wrap
    toc: true
    toc-title: "Contents"
    theme: solar
    # hide code?

execute:
  warning: false
  message: false
---


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](https://www.youtube.com/watch?v=ctc2kx3LxG8)


## 1. get a gemini api key

1. google: get a gemini api key
2. learn how to add [your key](https://ai.google.dev/gemini-api/docs/api-key?gclsrc=aw.ds&gad_source=1&gad_campaignid=20866959509&gbraid=0AAAAACn9t66lmk1KGr2izROD2Mys7MCHn&gclid=CjwKCAjwq9rFBhAIEiwAGVAZP3-PcughOmpUL6lFY5HYdCZoavUPG4a_3YPZbGUV6YM3HoCFmviAABoClU8QAvD_BwE) to your R environment.  
3. get your key [here](https://aistudio.google.com/app/apikey?_gl=1*cbrlbs*_ga*NzE5NjkzODIyLjE3NTY0MjY5ODk.*_ga_P1DBVKWT6V*czE3NTY4MzUwMDgkbzIkZzAkdDE3NTY4MzUxMTQkajI0JGwwJGgyMDM4MDUzNjg4)
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
```{r, eval = FALSE}
my_gemini_api_key = "XXXXXXXXXXXXX9FRPvvB85H_7YA0VqK_mlyiU94"
```

## 4. install ellmer
[ellmer](https://www.tidyverse.org/blog/2025/05/ellmer-0-2-0/): an R - LLM package

```{r, eval=FALSE}
 install.packages("ellmer")
```

then 

## 5. install usethis
this makes it easy to change settings
```{r, eval=FALSE}
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:

```{r, eval=FALSE}
usethis::edit_r_environ()
```

Now you should look to the Editor and add this line

```{r, eval=FALSE}
# in the .renviron file do
GEMINI_API_KEY=blah-blah-blah-whatever
```

then

## 7. restart R (click session)
then

## 8. load ellmer
```{r, eval=FALSE}
library(ellmer)
```

## 9.  then: choose a provider 
Use gemini unless you know what you're doing.  It's *free*.  Learn more [here](https://ai.google.dev/gemini-api/docs/pricing).

```{r, eval=FALSE}
chat = chat(name="google_gemini")
```

## 10. use it like this
```{r, eval=FALSE}
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
```{r, eval=FALSE}
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
```{r, eval=FALSE}
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
```{r, eval=FALSE}
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

```{r, eval=FALSE}
chat$chat("what is an interesting statistic about the palmerpenguins data")

# can you make a plot to display it?
```

