How to Add ChatGPT to RStudio

ChatGPT is a chatbot developed by OpenAI and launched in November 2022.

Since the release, ChatGPT has become very popular1 and is being used for a range of things including:

If you haven’t yet, you can create an account and start interacting with ChatGPT here.

For those who use R and RStudio, here is how you can bring ChatGPT into RStudio.

How to Add ChatGPT to RStudio?

1. Install the gptstudio package

Load the required packages:

Code
install.packages('pacman')
library(pacman)
pacman::p_load(devtools, usethis)
pacman::p_load_gh("MichelNivard/gptstudio")

Once gptstudio is installed3, the following steps must be completed before it will work.

2. Make an OpenAI account.

You need an OpenAI account to interact with ChatGPT.

Make an OpenAI account here.

3. Create an OpenAI API key

To use the openai package within Rstudio, you need an OpenAI API Key.

To create an OpenAI API key, go to the OpenAI API key page here.

Click on Create new secret key.

A message will pop up to save the secret key.

Copy the secret key and store it somewhere safe, then click OK.

4. Set the API key in Rstudio

For RStudio to connect to ChatGPT, the OpenAI API key must be set within RStudio.

To set the API key you can either:

a) Set a global environmental variable.

To set a global environment variable, you can use the following command, replacing "<APIKEY>" with your actual secret key:

Code
Sys.senv(OPENAI_API_KEY = "<APIKEY>")

b) Edit the .Renviron file

I. User specific

Open the .Renviron file with help of the usethis package:

Code
usethis::edit_r_environ()

Add the following line to the file, replacing "<APIKEY>" with your actual secret key:

OPENAI_API_KEY = <APIKEY>

This API key is now accessible in all your projects which means ChatGPT will work in all your projects.

II. Project specific

Open the .Renviron file with usethis:

Code
usethis::edit_r_environ(scope = 'project')

Add the following line to the file, again replacing "<APIKEY>" with your actual key:

OPENAI_API_KEY = <APIKEY>

The .Renviron file will be stored in the current project folder and the API key will only be accessible to the current project.

This means ChatGPT will only work in the current project.

Note: If you are using GitHub/GitLabs, do not forget to add .Renviron to .gitignore!

How to use ChatGPT in RStudio?

The gptstudio options can be accessed through the RStudio Addins menu.

1. GPT Chat

Selecting GPT Chat allows you to interact with ChatGPT directly within RStudio.

It feels similar to the web app version of ChatGPT and means you don’t have to login to ChatGPT with a web browser4.


2. Spelling and Grammar

The Spelling and Grammar option is self explanatory and appears to work well.

For example, I ran Spelling and Grammar on the following text:

Hello ths is a speling check example with ChatGPT inctalled in RStudio.

And this is what it returned:

Hello this is a spelling check example with ChatGPT installed in RStudio.

3. Change Text to Active Voice

The Change Text to Active Voice is also self explanatory and again, appears to work well.

For example, I gave it this text:

The rubbish was picked up by the man in the hat.

And it returned this:

The man in the hat picked up the rubbish.

4. Write/Code from Prompt

The Write/Code from Prompt option takes a chunk of highlighted text and attempts to write code.

For example, I gave it this text:

Use ggplot to make a scatter plot of mpg vs. hp using the mtcars dataset

And it wrote the following code:

Code
library(ggplot2)

ggplot(data = mtcars, aes(x = hp, y = mpg)) +
  geom_point() 

Not bad at all! 😲

5. Comment your Code

The Comment your code option takes a selected code chunk and returns the same code with added comments.

For example, I gave it this code chunk:

Code
library(ggplot2)

ggplot(data = mtcars, aes(x = hp, y = mpg)) +
  geom_point()

And it returned this:

Code
library(ggplot2) #loads the ggplot2 package

ggplot(data = mtcars, aes(x = hp, y = mpg)) + #creates a scatterplot of mpg vs hp
  geom_point() #adds points to the scatterplot

Again not bad but my preference is to have comments before a line of code, not next to it. 😉

6. Freeform GPT Editor

Freeform GPT Editor is probably the most powerful option because it lets you apply any edit to a selection of code or text.

For example, I highlighted the code mtcars[, c("mpg","cyl"] and asked the Freeform GPT Editor to “Convert the code into Tidyverse syntax”.

Here is what it produced:

Very impressive! 😦

7. Specify Model Editor

The Specify Model Editor does not appear to come with instructions.

However, I decided to give it a try.

I asked it to predict mpg by hp and cyl in the mtcars data set.

Here is what it came up with:

Again, not bad! 👏🏼

Final Thoughts

ChatGPT is not only fun, but is incredibly useful when developing code.

I thought adding ChatGPT to RStudio would be a bit of a gimmick.

But boy I was wrong!

gptstudio brings the power of ChatGPT into RStudio and adds some impressive functionality.

Thanks to Nivard and Wade (2023) who developed the gptstudiopackage.

Resources

Image by rawpixel.com on Freepik

References

Nivard, Michel, and James Wade. 2023. “Gptstudio: Gptstudio Adds Functionality Based on OpenAI’s GPT Family of Models Directly into Rstudio.”

Footnotes

  1. It is estimated that ChatGPT had 100 million active users in January 2023, only two months after launch.↩︎

  2. chatgpt-30-incredible-ways-to-use↩︎

  3. RStudio may need to be restarted for gptstudio to appear in the Addins menu.↩︎

  4. Sometimes the standard ChatGPT web interface is not accessible due to ‘being at capacity’. I find when this happens I can still interact with ChatGPT via the RStudio Addin.↩︎