8 ChatGPT resources for R programming
5 min read
Table of Contents
Without having wonderful-tuning or being educated on a certain topic, ChatGPT can respond to concerns about a huge selection of technological innovation subjects—including how to create R code. That indicates ChatGPT’s power is obtainable to any R programmer, even one particular who appreciates minor about huge language designs. (A big language model, or LLM, is the technological know-how underpinning AI chatbots like OpenAI’s ChatGPT.)
An ecosystem is forming all over ChatGPT and R, making it simple to include the AI know-how into your R language workflow. But right before you begin using ChatGPT and instruments affiliated with it for tasks in R, there are a several significant things to retain in thoughts:
- Almost everything you question with these tools receives sent to OpenAI’s servers. Will not use ChatGPT tools to course of action sensitive information and facts.
- ChatGPT may well confidently return solutions that are incorrect. Even incorrect responses can be a time-conserving beginning place, but never think the code will do specifically what you be expecting. Kyle Walker, an associate professor at Texas Christian University and writer of the well-known
tidycensus
R bundle, a short while ago tweeted that ChatGPT can “supercharge your function if you fully grasp a subject properly,” or it can leave you “uncovered for not figuring out what you are performing.” The variance is in understanding when the AI output just isn’t ideal. Usually look at ChatGPT’s responses. - ChatGPT can create unique responses to the similar query—and some responses could be accurate though many others usually are not. For occasion, when I asked various occasions for a
ggplot2
bar chart with blue bars, the code generated a graph with blue bars in some cases but not many others, even nevertheless I submitted the correct exact request. This is certainly less than perfect if you will need a reproducible workflow. - If there’s been a modern update to a deal you are using, ChatGPT will not know about it, due to the fact its education details finishes in 2021.
- Most of the sources in this article need you to have your individual OpenAI API key, and the API just isn’t totally free to use. Whilst pricing is low at the moment, there’s no warranty it will remain that way. Present pricing is 2 cents for every 10,000 tokens for the ChatGPT 3.5 turbo product. What does a token get you? As 1 instance, the request to build a scatter plot from a 234-row mpg information set value 38 tokens, a fraction of a cent.
- Asking ChatGPT for coding assistance is not likely to ensnare you in the ethics of AI racial and gender bias. On the other hand, there are heated discussions about the knowledge of furnishing OpenAI with still a lot more data the ethics of how the training data was scraped and repurposed and if it really is superior to use open up resource big language designs (these as H2O.ai’s h2oGPT) rather than OpenAI’s. Individuals dilemmas are for each unique and corporation to parse for themselves. However, as of this crafting, there just are not R-specific LLM applications that are comparable to all those making up close to ChatGPT.
Now, let’s glimpse at some of the most noteworthy R-concentrated ChatGPT methods presently offered.
RTutor
This application is an exquisite and effortless way to sample ChatGPT and R. Upload a data established, check with a question, and view as it generates R code and your success, together with graphics. Although it’s named RTutor, the app can also deliver Python code.
RTutor is on the web at https://rtutor.ai/. It is currently the only app or bundle detailed that doesn’t call for a ChatGPT API key to use, but you’re questioned to provide your possess for hefty use so as not to bill the creators’ account.
Figure 1. Final results when asking RTutor to build a bar chart
The app’s About site explains that RTutor’s major goal “is to support persons with some R knowledge to discover R or be additional productive … RTutor can be utilized to speedily pace up the coding method employing R. It presents you a draft code to exam and refine. Be wary of bugs and glitches.”
The code for RTutor is open up resource and offered on GitHub, so you can install your very own area model. However, licensing only lets use of the application for nonprofit or non-industrial use, or for industrial testing. RTutor is a individual job of Dr. Steven Ge, a professor of bioinformatics at South Dakota State University.
CodeLingo
This multi-language application “interprets” code from a person programming language to another. Available languages include things like Java, Python, JavaScript, C, C++, PHP and a lot more, which includes R. This is a web software only, offered at https://analytica.shinyapps.io/codelingo/ . You will need to enter your OpenAI API critical to use it (you could want to regenerate the important right after testing).
Figure 2. ChatGPT in the CodeLingo application tries to translate ggplot2 graph code to Python
A request to translate code for a ggplot2 R graph into JavaScript created output making use of the somewhat challenging-to-study D3 JavaScript library, as opposed to one thing a JavaScript novice would be more probable to want these kinds of as Observable Plot or Vega-Lite.
The ask for to translate into Python, revealed in Determine 2, was far more straightforward and applied libraries I’d be expecting. On the other hand, ChatGPT didn’t comprehend that “Established1” is a ColorBrewer colour palette and won’t be able to be utilised specifically in Python. As is the case for a lot of ChatGPT employs, translating code in between programming languages might give you a practical starting off level, but you will need to know how to correct faults.
The application was designed by Analytica Information Science Options.
askgpt
This package, readily available at https://github.com/JBGruber/askgpt, can be a fantastic commencing issue for very first-time users who want ChatGPT in their console, in element due to the fact it offers some instructions upon initial startup. Load the offer with library(askgpt)
and it responds with:
Hello, this is askgpt ☺.
• To start off error logging, run `log_init()` now.
• To see what you can do use `?askgpt()`.
• Or just run `askgpt()` with any query you want!
Use the login()
function without the need of initially storing a critical, and you are going to see a information on how to get an API critical:
ℹ It looks like you have not provided an API critical yet.
1. Go to
2. (Log into your account if you haven't completed so yet)
3. On the web-site, simply click the button + Produce new mystery key to produce an API essential
4. Duplicate this key into R/RStudio
You can be questioned to help save your crucial in your keyring, and then you are all established for upcoming sessions. If your important is already stored, login()
returns no information.
askgpt
‘s default is to retailer results of your query as an item so you can help save them to a variable like this one:
barchart_directions <- 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, 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 sibling, 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 website, 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 for asking 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.
Figure 3. Querying gptstudio's ChatGPT add-in
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, voilà! 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:
Figure 4. Example of the ChatGPT in Source add-in
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 still could 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 Script to Function
- Add roxygen to Function (documents a function)
- Suggest Unit Test
- 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> Display Command Palette in the RStudio Addins menu or Ctrl-Shift-P on Windows, or Cmd-Shift-P on a Mac).
When I ran an incorporate-in, I failed to often see a concept telling me that one thing was taking place, so be affected individual.
The Propose Enhancements insert-in created uncommented textual content underneath my operate in an R file adopted by modified code. Some of the ideas were not really handy. For instance, 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 suggestion 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
file with
OPENAI_API_KEY="your key"
and you're good to go. If you attempt to run one of the add-ins before storing your key, you'll get an error message telling you how to do the key setup.