Code
install.packages('pacman')
library(pacman)
::p_load(devtools, usethis)
pacman::p_load_gh("MichelNivard/gptstudio") pacman
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:
answering simple questions
having philosophical conversations
composing essays
writing code
And much more2
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.
gptstudio
packageLoad the required packages:
install.packages('pacman')
library(pacman)
::p_load(devtools, usethis)
pacman::p_load_gh("MichelNivard/gptstudio") pacman
Once gptstudio
is installed3, the following steps must be completed before it will work.
You need an OpenAI account to interact with ChatGPT.
Make an OpenAI account here.
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.
For RStudio to connect to ChatGPT, the OpenAI API key must be set within RStudio.
To set the API key you can either:
To set a global environment variable, you can use the following command, replacing "<APIKEY>"
with your actual secret key:
Sys.senv(OPENAI_API_KEY = "<APIKEY>")
Open the .Renviron file with help of the usethis
package:
::edit_r_environ() usethis
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.
Open the .Renviron file with usethis
:
::edit_r_environ(scope = 'project') usethis
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!
The gptstudio
options can be accessed through the RStudio Addins menu.
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.
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.
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.
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:
library(ggplot2)
ggplot(data = mtcars, aes(x = hp, y = mpg)) +
geom_point()
Not bad at all! 😲
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! 😦
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! 👏🏼
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 gptstudio
package.
Image by rawpixel.com on Freepik
It is estimated that ChatGPT had 100 million active users in January 2023, only two months after launch.↩︎
RStudio may need to be restarted for gptstudio
to appear in the Addins menu.↩︎
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.↩︎
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
And it returned this:
Code
Again not bad but my preference is to have comments before a line of code, not next to it. 😉