Without fine-tuning or being trained on a specific topic, ChatGPT can answer questions about a wide range of technology subjects—including how to write R code. That means ChatGPT’s power is available to any R programmer, even one who knows little about large language models. (A large language modelor LLM, is the technology underpinning AI chatbots like OpenAI’s ChatGPT.)
An ecosystem is forming around ChatGPT and R, making it easy to incorporate the AI technology into your R language workflow. But before you start using ChatGPT and the tools associated with it for projects in R, there are a few important things to keep in mind:
- Everything you ask with these tools gets sent to OpenAI’s servers. Don’t use ChatGPT tools to process sensitive information.
- ChatGPT may confidently return answers that are wrong. Even incorrect responses can be a time-saving starting point, but don’t assume the code will do exactly what you expect. Kyle Walker, an associate professor at Texas Christian University and author of the popular
tidycensus
R package, recently tweeted that ChatGPT can “supercharge your work if you understand a topic well,” or it can leave you “exposed for not knowing what you are doing.” The difference is in knowing when the AI output isn’t right. Always check ChatGPT’s responses. - ChatGPT can generate different responses to the same query—and some answers might be accurate while others aren’t. For instance, when I asked multiple times for a
ggplot2
bar chart with blue bars, the code generated a graph with blue bars sometimes but not others, even though I submitted the exact same request. This is obviously less than ideal if you need a reproducible workflow. - If there’s been a recent update to a package you’re using, ChatGPT won’t know about it, since its training data ends in 2021.
- Most of the resources in this article require you to have your own OpenAI API key, and the API isn’t free to use. While pricing is low at the moment, there’s no guarantee it will stay that way. Current pricing is 0.2 cents per 10,000 tokens for the ChatGPT 3.5 turbo model. How to say a token get you? As one example, the request to create a scatter plot from a 234-row mpg data set costs 38 tokens, a fraction of a cent.
- Asking ChatGPT for coding help is unlikely to catch you in the ethics of racial and gender bias AI. However, there are heated discussions about the wisdom of furnishing OpenAI with yet more data; the ethics of how the training data was scraped and repurposed; and if it’s better to use open source large language models (such as H2O.ai’s h2oGPT) rather than OpenAI’s. Those dilemmas are for every individual and organization to parse for themselves. However, as of this writing, there simply aren’t R-specific LLM tools that are comparable to those building up around ChatGPT.
Now, let’s look at some of the most notable R-focused ChatGPT resources currently available.
RTutor
This app is an elegant and easy way to sample ChatGPT and R. Upload a data set, ask a question, and watch as it generates R code and your results, including graphics. Although it’s named RTutor, the app can also generate Python code.
RTutor is on the web at https://rtutor.ai/. It’s currently the only app or package listed that doesn’t require a ChatGPT API key to use, but you’re asked to supply your own for heavy use so as not to bill the creators’ account.
The app’s About page explains that RTutor’s primary goal “is to help people with some R experience to learn R or be more productive … RTutor can be used to quickly speed up the coding process using R. It gives you a draft code to test and refine. Be wary of bugs and errors.”
The code for RTutor is open source and available on GitHub, so you can install your own local version. However, licensing only allows use of the app for non-profit or non-commercial use, or for commercial testing. RTutor is a personal project of Dr. Steven Ge, a professor of bioinformatics at South Dakota State University.
CodeLingo
This multi-language app “translates” code from one programming language to another. Available languages include Java, Python, JavaScript, C, C++, PHP and more, including R. This is a web application only, available at https://analytica.shinyapps.io/codelingo/ . You need to input your OpenAI API key to use it (you may want to regenerate the key after testing).
A request to translate code for a ggplot2 R graph into JavaScript generated output using the rather hard-to-learn D3 JavaScript library, as opposed to something a JavaScript newbie would be more likely to want such as Observable Plot or Vega-Lite.
The request to translate into Python, shown in Figure 2, was more straightforward and used libraries I’d expect. However, ChatGPT didn’t understand that “Set1” is a ColorBrewer color palette and can’t be used directly in Python. As is the case for many ChatGPT uses, translating code between programming languages may give you a useful starting point, but you will need to know how to fix mistakes.
The app was created by Analytica Data Science Solutions.
askkgpt
This package, available at https://github.com/JBGruber/askgpt, can be a good starting point for first-time users who want ChatGPT in their console, in part because it gives some instructions upon initial startup. Load the package with library(askgpt)
and it responds with:
Hi, this is askgpt ☺.
• To start error logging, run `log_init()` now.
• To see what you can do use `?askgpt()`.
• Or just run `askgpt()` with any question you want!
use the login()
function without first storing a key, and you’ll see a message on how to get an API key:
ℹ It looks like you have not provided an API key yet.
1. Go to <https://platform.openai.com/account/api-keys>
2. (Log into your account if you haven't done so yet)
3. On the site, click the button + Create new secret key to create an API key
4. Copy this key into R/RStudio
You’ll be asked to save your key in your keyring, and then you’re all set for future sessions. If your key is already stored, login()
returns no message.
askgpt
‘s default is to store results of your query as an object so you can save them to a variable like this one:
barchart_instructions <- askgpt("How do I make a bar chart with custom colors with ggplot2?")
Submit a query and you’ll first see:
GPT is thinking ⠴
This way, you know your request has been sent and an answer should be forthcoming, instead of wondering what is happening after you hit submit.
Along with the package’s general askgpt()
function, there are a few coding-specific functions such as annotate_code()
, explain_code()
and test_function()
. These will involve cutting and pasting responses back into your source code.
For those familiar with the OpenAI API, the package’s chat_api()
function allows you to set API parameters such as the model you want to use, the maximum tokens you’re willing to spend per request, and your desired response temperature (which I’ll explain in more detail later in the article).
the chat_api()
function returns a list, with the text portion of the response in YourVariableName$choices[[1]]$message$content
. Other useful info is stored in the list, as well, such as the number of tokens used.
the askgpt
package was created by Johannes Gruber, a post-doc researcher at Vrije Universiteit Amsterdam. It can be installed from CRAN.
gptstudio
This package and its siblings, gpttools
(discussed below), feature RStudio add-ins to work with ChatGPT, although there are also some command-line functions that will work in any IDE or terminal.
You can access add-ins within RStudio either from the add-in drop-down menu above the code source pane or by searching for them via the RStudio command palette (Ctrl-shift-p).
According to the package site, gptstudio
is a general-purpose helper “for R programmers to easily incorporate use of large language models (LLMs) into their project workflows.” It is on CRAN.
One add-in, ChatGPT, launches a browser-based app to ask your R coding questions, and offers options for programming style (tidyverse, base, or no preference) and proficiency (beginner, intermediate, advanced, and genius).
In the screenshot below, I’ve asked how to create a scatter plot in R as an intermediate coder with a tidyverse style.
Asking the same question with the base programming style produced code using base R’s plot function as the answer.
Although designed for R coding help, gptstudio
can tap into more ChatGPT capabilities, so you can ask it anything that you would the original web-based ChatGPT. For instance, this app worked just as well as a ChatGPT tool to write Python code and answer general questions like, “What planet is farthest away from the sun?”
Another of the gptstudio
package’s add-ins, ChatGPT in Source, seems closest to magic. You write code as usual in your source pane, add a comment requesting changes you’d like in the code, select the block of code including your comment, and apply the add-in. then, voila! Your requested changes are made.
When I applied the add-in to this code:
# Sort bars by descending Y value, rotate x-axis text 90 degrees, color bars steel blue
ggplot(states, aes(x = State, y = Pop_2020)) +
geom_col()
My code was replaced with what is shown in the highlighted selection of Figure 4:
That’s cool . . . except if you run this code, the bars won’t display as steel blue. moving fill = "steelblue"
inside geom_col()
makes it work. That mistake has nothing to do with this specific add-in, but with the vagaries of ChatGPT itself. As I previously mentioned, I’ve run the same request other times and the results were accurate.
Sending the following code to the ChatGPT in Source add-in generated complete instructions and code for a Shiny app:
# Create an R Shiny app with this data
states <- readr::read_csv("https://raw.githubusercontent.com/smach/SampleData/main/states.csv")
Submitting my request twice returned two completely different results, however—the first with a two-file app that forgot to load the ggplot2
library before using it; the second calling columns that weren’t actually in the data. It takes more work to craft a query that handles the specifics of an existing data set, but the code can still serve as a framework to build on.
gptstudio
was written by Michel Nivard and James Wade.
gpttools
The aim of the gpttools
package “is to extend gptstudio
for R package developers to more easily incorporate use of large language models (LLMs) into their project workflows,” according to the package website. The gpttools
package isn’t on CRAN as of this writing. Instead, you can install gpttools
from the JamesHWade/gpttools GitHub repo or R Universe with the following:
# Enable repository from jameshwade
options(repos = c(
jameshwade = "https://jameshwade.r-universe.dev",
CRAN = "https://cloud.r-project.org"
))
# Download and install gpttools in R
install.packages("gpttools")
The package’s add-ins include:
- ChatGPT with Retrieval
- Convert Scripts to Functions
- Add roxygen to Function (documents a function)
- Suggested Unit Tests
- Document Data
- Suggest Improvements
To run an add-in, highlight your code and then select the add-in either from the RStudio Addins dropdown menu or by searching for it in the command palette (Tools > Show Command Palette in the RStudio Addins menu or Ctrl-Shift-P on Windows, or Cmd-Shift-P on a Mac).
When I ran an add-in, I didn’t always see a message telling me that something was happening, so be patient.
The Suggest Improvements add-in generated uncommented text below my function in an R file followed by modified code. Some of the suggestions weren’t very helpful. For example, for this code
if (exportcsv)
filename_root <- strsplit(filename, "\\.")[[1]][1]
filename_with_winner <- paste0(filename_root, "_winners.csv")
rio::export(data, filename_with_winner)
the add-in recommended
Use `paste()` instead of `paste0()` to ensure a space is included between the names of the winners.
I didn’t want a space in my file name! Still, I couldn’t argue with all of its advice. The following suggestions seemed reasonable:
Use a switch statement instead of multiple if statements, to allow for additional functionality in the future
In this case, I’d be more likely to use dplyr’s case_when()
or data. table’s fcase()
than base R’s switch()
.
Make sure you have an original copy of your code if you’re using any package’s ChatGPT add-in, since there is a risk of code being overwritten in a way you don’t necessarily want.
chatgpt
The chatgpt R package offers both functions and RStudio add-ins for using ChatGPT in R, with 10 add-ins documented at the time I tested.
Code-specific functions include comment_code()
, complete_code(
), create_unit_tests()
, document_code()
, find_issues_in_code()
and refactor_code()
. There’s also a generic ask_chatgpt()
function and add-in if you’d like to use ChatGPT for something not code-related.
Store your key in your .Renviron
files with
OPENAI_API_KEY="your key"
and you’re good to go. If you attempt to run one of the add-ins before saving your key, you’ll get an error message telling you how to do the key setup.