8 ChatGPT tools for R programming

The generative power of OpenAI's GPT 3.5 LLM is now available to R users, with a growing collection of ChatGPT packages and apps to choose from.

1 2 Page 2
Page 2 of 2

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 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.

The package is on CRAN, or you can install the development version with


remotes::install_github("jcrodriguez1989/chatgpt", build_vignettes = TRUE)

When I tried an add-in without loading the package first, nothing happened. I then loaded the package with library(chatgpt) and got this message:


Warning message:
In run_addin("document_code") :
  Please set one of `OPENAI_ADDIN_REPLACE=TRUE` or `OPENAI_VERBOSE=TRUE` 

I followed the instructions in my R environment file, setting the verbose option to TRUE as I didn't want my initial code to be replaced.

That resulted in a query to ChatGPT being displayed in my console and a response also being displayed in the console. 

With the option OPENAI_ADDIN_REPLACE=TRUE in my R environment file and my code selected in RStudio, some of my initial code occasionally disappeared when documentation was added. I ended up sticking with this package's command-line functions instead of the add-ins, but you might find the add-ins useful—just remember to make a copy of your code before experimenting. 

The chatgpt package was created by Juan Cruz Rodriguez.

gptchatteR

Billed as "an experimental and unofficial wrapper for interacting with OpenAI GPT models in R," one advantage of gptchatteR is its chatter.plot() function.

Install the package with


remotes::install_github("isinaltinkaya/gptchatteR", build_vignettes = TRUE, dependencies = TRUE)

This ensures that it also installs the required openai package. Then, you can load the package and authenticate with


library(gptchatteR)
chatter.auth("YOUR KEY")

Once that's done, launch a chat session with chatter.create().

The chatter_create() arguments include a model for the OpenAI model (default is text-davinci-003), max_tokens for the maximum number of tokens you want it to use (default is 100), and a "temperature" set with an argument like this one:


chatter.create(temperature = 0)

According to the OpenAI documentation, the temperature setting can be between 0 and 1 and represents "how often the model outputs a less likely token."

The higher the temperature, the more random (and usually creative) the output. This, however, is not the same as "truthfulness." For most factual use cases such as data extraction, and truthful Q&A, the temperature of 0 is best.

The package default is a neutral 0.5. Unless you want to be entertained as opposed to getting usable code, it's worth setting your temperature to 0.

As of when I tested, the package was working but generated this warning:


The `engine_id` argument of `create_completion()` is deprecated as of openai 0.3.0.
ℹ Please use the `model` argument instead.
ℹ The deprecated feature was likely used in the gptchatteR package.
  Please report the issue to the authors.
  

You can create a "casual" chat with chatter.chat("Your input here"). If you think you'll want follow-up after your initial request, use chatter.feed(), which stores your first query for use in a second question, and so on.

After running the following code:


library(gptchatteR)
chatter.auth(Sys.getenv("OPENAI_API_KEY"))
chatter.create(temperature = 0)
chatter.feed('I have the following data in R mydf <- data.frame(State = c("CT", "NJ", "NY"), Pop = c(3605944, 9288994, 20201249))')
myplot <- chatter.plot("Make a graph with State on the x axis and Pop on the Y axis")

a graph appeared in my RStudio view pane. The graph code was stored in myplot$code.

The gptchatteR package was created by Isin Altinkaya, a PhD fellow at the University of Copenhagen.

One more ...

That's the top eight ChatGPT packages for R. Here's one more—and I will keep adding to this list, so check back in the future.

chatgptimages wasn't designed to help you code. Instead, it uses a familiar R and Shiny interface to access another ChatGPT capability: creating images. There are a number of ethical intellectual property issues currently tangled up in AI image creation based on what was used to train models, which is important to keep in mind if you want to use this package for anything beyond entertainment.

That said, if you'd like to give it a try, note that it doesn't install like a usual package. First, make sure you also have shiny, golem, shinydashboard, openai, config, and testthat installed on your system. Then, fork and download the entire GitHub repo at https://github.com/analyticsinmotion/chatgpt-images-r-shiny or download and unzip the .zip file from https://github.com/analyticsinmotion/chatgpt-images-r-shiny. Open the chatgptimages.Rproj file in RStudio, open the run_dev.R file in the project's dev folder, and run that short file line by line. This app should open in your default browser:

Shiny dashboard with instructions on how to use the app: enter API, click create image tab Screen shot by Sharon Machlis

Figure 6. The chatgptimages app running in a browser

Follow the instructions on storing a ChatGPT API key, and you can start creating and saving images.

The results look something like what's shown in Figure 7.

Painting-like image from request 'Create a scene of the Atlantic Coast in the style of Homer'Screen shot by Sharon Machlis

Figure 7. A saved image from chatgptimages

Beyond ChatGPT

If you'd like to test out other large language models that are open source, one non-R-specific tool, Chat with Open Large Language Models, is interesting. It offers access to nine different models as of this writing and an "arena" where you can test two at once and vote for the best.

Be aware of the terms of use: "non-commercial use only. It only provides limited safety measures and may generate offensive content. It must not be used for any illegal, harmful, violent, racist, or sexual purposes. The service collects user dialogue data for future research."

As a final note, H2o.ai has a website where you can test its model at https://gpt.h2o.ai/.

Copyright © 2023 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2