Skip to content
R for the Rest of Us Logo

Using AI with R

Using AI in RStudio

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

View code shown in video
# Load Packages -----------------------------------------------------------

library(tidyverse)

# Import Data -------------------------------------------------------------

survey_english <-
  read_tsv("https://raw.githubusercontent.com/rstudio/r-community-survey/refs/heads/master/2020/data/2020-english-survey-final.tsv")

# Data Analysis -----------------------------------------------------------

# Count the responses on the Qr_experience variable, grouped by Qr_how_often_used
# Keep the result in a tidy format
# Use the native pipe (|>) not the tidyverse pipe (%>%)

survey_english |> 
  count(Qr_how_often_used, Qr_experience) |>
  ggplot(aes(x = Qr_how_often_used, y = n, fill = Qr_experience)) +
  geom_col(position = "dodge") +
  labs(title = "QR Code Experience by Frequency of Use",
       x = "Frequency of Use",
       y = "Count") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_fill_brewer(palette = "Set1")


# {elmer} -----------------------------------------------------------------

library(elmer)

chat <- chat_openai()

chat$chat("Who invented R?")
chat$chat("What is the easiest way to make a histogram in R?")

data <- c(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5)

# Create a histogram
hist(data, 
     main = "Histogram of Sample Data", 
     xlab = "Values", 
     ylab = "Frequency", 
     col = "lightblue", 
     border = "black")

Learn More

RStudio and GitHub Copilot

If you need to download the newest version of RStudio, you can do that on the Posit website.

If you want to see the R community survey data, you can find it in this GitHub repo.

Information about GitHub Copilot can be found on the GitHub website.

If you want to go really in depth on using GitHub Copilot in RStudio, check out Tom Mock's 2023 talk GitHub Copilot in Rstudio, it's finally here!

{ellmer}

To get an OpenAI API key in order to work with the {ellmer} package, go to the OpenAI platform website.

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Alberto Cabrera

Alberto Cabrera • March 14, 2025

I was unable to install Ellmer. I secured an IP, but it was not recognized.

David Keyes

David Keyes Founder • March 14, 2025

Did install.packages("ellmer") not work for you to install {ellmer}? You can use that now that the package is on CRAN.

Alberto Cabrera

Alberto Cabrera • March 14, 2025

Thanks for the prompt reply. I was able to install ellmer. However, the procedure to link it with an IP did not work. I even got an API key from OPENAI. I also modified the .Rhistory with the following: Sys.setenv(OPENAI_API_KEY = " " to no avail. However, I was able to use the CoPilot thanks to your lesson.

David Keyes

David Keyes Founder • March 14, 2025

Make sure you add your API key to the .Renviron file (not the .Rhistory).

Alberto Cabrera

Alberto Cabrera • March 14, 2025

I just replicated the steps described in the lesson. Created an .Renviron with the key.

usethis::edit_r_environ() ☐ Modify /Users/albertocabrera/.Renviron. ☐ Restart R for changes to take effect.

Restarted the session. Created the object chat which produced the following output.

library(ellmer) chat <- chat_openai() Using model = "gpt-4o". Error in openai_key(): ! Can't find env var OPENAI_API_KEY. Run rlang::last_trace() to see where the error occurred.

I also made certain I had signed to GitHub, while calling to a project.

David Keyes

David Keyes Founder • March 17, 2025

Hmm, strange. Can you record a video here to show me the exact steps you're following so I can try to debug?

Elena Getici

Elena Getici • March 23, 2025

I followed your instructions however when I run the chat <- chat_openai() and chat$chat("Who invented R?") functions it gives me the following:

library(ellmer) chat <- chat_openai() Using model = "gpt-4o". Error in openai_key(): ! Can't find env var OPENAI_API_KEY. Run rlang::last_trace() to see where the error occurred. chat$chat("Who invented R?") Errore: oggetto 'chat' non trovato

David Keyes

David Keyes Founder • March 25, 2025

Did you create an API key and add it to your .Renviron file?

Elena Getici

Elena Getici • March 25, 2025

Yes I did, I followed the instructions in the video

David Keyes

David Keyes Founder • March 25, 2025

Could I ask you to record a video while you run the steps again? You can do that here. That will help me help you to debug.

Blayne Beacham

Blayne Beacham • April 8, 2025

I'm getting the same error as the two above. I'm guessing it might have to do with IT restrictions on changing environmental variables. Did you get to the bottom of it for the other two?

David Keyes

David Keyes Founder • April 9, 2025

We never did. If you wouldn't mind sending me a video to show me what you're seeing, that would be great.

Blayne Beacham

Blayne Beacham • April 9, 2025

It appears that I can use the Sys.setenv(OPENAI_API_KEY = "[api_key]") function to set the environmental variable for the session (as opposed to the .Renviron approach) and then proceed to the next step. However, I do get a different error while sending a prompt. This could also be due to IT restrictions at my work so I'll need to try it at home on my personal computer. I'll let you know.