Skip to content

nicholas-masel/chatgpt

 
 

Repository files navigation

ChatGPT coding assistant for RStudio

Meme by Programming Jokes I IT Humor & Memes

Installation

You can install the development version of {chatgpt} from GitHub with:

# install.packages("remotes")
remotes::install_github("jcrodriguez1989/chatgpt")

Requirements

You need to setup your ChatGPT API key in R.

First you will need to obtain your ChatGPT API key. You can create an API key by accessing OpenAI API page -don’t miss their article about Best Practices for API Key Safety-.

Then you have to assign your API key for usage in R, this can be done just for the actual session, by doing:

Sys.setenv(OPENAI_API_KEY = "XX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")

Or you can do it persistent (session-wide), by assigning it in your .Renviron file. For it, execute usethis::edit_r_environ(), and in that file write a line at the end your API key as

OPENAI_API_KEY=XX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Features

The {chatgpt} R package provides a set of features to assist in R coding. Current existing addins:

  • Ask ChatGPT: Opens an interactive chat session with ChatGPT
  • Comment selected code: Comment the selected code
  • Create variable name: Create a name for a variable that would be assigned the result of this code
  • Document code (in roxygen2 format): Document a function definition, in roxygen2 format
  • Explain selected code: Explain the selected code
  • Find issues in the selected code: Find issues in the selected code
  • Optimize selected code: Optimize the selected code
  • Refactor selected code: Refactor the selected code

Note: When no code is selected, it will use the whole file’s code.

Code Examples

comment_code

> cat(comment_code("for (i in 1:10) {\n  print(i ** 2)\n}"))

*** ChatGPT input:

Add inline comments to the following R code: "for (i in 1:10) {
  print(i ** 2)
}"
# Loop through the values 1-10
for (i in 1:10) {
  # Print the squared value of i
  print(i ** 2)
}

create_unit_tests

> cat(create_unit_tests("squared_numbers <- function(numbers) {\n  numbers ^ 2\n}"))

*** ChatGPT input:

Create a full testthat file, with test cases for the following R code: "squared_numbers <- function(numbers) {
  numbers ^ 2
}"
library(testthat)

context("Test squared_numbers function")

# Test 1: Check if function works with a single input
test_that("Squared numbers should work with a single input", {
  expect_equal(squared_numbers(2), 4)
})

# Test 2: Check if function works with a vector of inputs
test_that("Squared numbers should work with a vector of inputs", {
  expect_equal(squared_numbers(c(1, 2, 3)), c(1, 4, 9))
})

# Test 3: Check if function works with a matrix of inputs
test_that("Squared numbers should work with a matrix of inputs", {
  expect_equal(squared_numbers(matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)), 
               matrix(c(1, 4, 9, 16), nrow = 2, ncol = 2))
})

create_variable_name

> cat(create_variable_name("sapply(1:10, function(i) i ** 2)"))

*** ChatGPT input:

Give a good variable name to the result of the following R code: "sapply(1:10, function(i) i ** 2)"
squared_values

document_code

> cat(document_code("square_numbers <- function(numbers) numbers ** 2"))

*** ChatGPT input:

Document, in roxygen2 format, this R function: "square_numbers <- function(numbers) numbers ** 2"
'#' Square Numbers
'#'
'#' Computes the square of a number
'#'
'#' @param numbers the number to be squared
'#' @return the squared number
'#' @export
square_numbers <- function(numbers) {
  numbers ** 2
}

explain_code

> cat(explain_code("for (i in 1:10) {\n  print(i ** 2)\n}"))

*** ChatGPT input:

Explain the following R code: "for (i in 1:10) {
  print(i ** 2)
}"
This code will print the squares of the numbers 1 to 10. The for loop will iterate over the numbers 1 to 10, and for each number, the code will print the result of that number raised to the power of 2 (i.e. the square of that number).

find_issues_in_code

> cat(find_issues_in_code("i <- 0\nwhile (i < 0) {\n  i <- i - 1\n}"))

*** ChatGPT input:

Find issues or bugs in the following R code: "i <- 0
while (i < 0) {
  i <- i - 1
}"
1. The while loop is always false, since the starting value of i is 0 and the code is checking if it is less than 0. It should be while (i > 0).

2. The value of i is not modified inside the loop, so the loop will never terminate. It should be i <- i - 1.

optimize_code

> cat(optimize_code("i <- 10\nwhile (i > 0) {\n  i <- i - 1\n  print(i)\n}"))

*** ChatGPT input:

Optimize the following R code: "i <- 10
while (i > 0) {
  i <- i - 1
  print(i)
}"
for (i in 10:0) {
  print(i)
}

refactor_code

> cat(refactor_code("i <- 10\nwhile (i > 0) {\n  i <- i - 1\n  print(i)\n}"))

*** ChatGPT input:

Refactor the following R code, returning valid R code: "i <- 10
while (i > 0) {
  i <- i - 1
  print(i)
}"
for(i in 10:1){
  print(i)
}

Additional Parameters

Disable Console Messages

If you want {chatgpt} not to show messages in console, please set the environment variable OPENAI_VERBOSE=FALSE.

Addin Changes in Place

If you want {chatgpt} addins to take place in the editor -i.e., replace the selected code with the result of the addin execution- then you sould set the environment variable OPENAI_ADDIN_REPLACE=TRUE.

ChatGPT Model Tweaks

ChatGPT model parameters can be tweaked by using environment variables.

The following environment variables variables can be set to tweak the behavior, as documented in https://beta.openai.com/docs/api-reference/completions/create .

  • OPENAI_MODEL; defaults to "text-davinci-003"
  • OPENAI_MAX_TOKENS; defaults to 256
  • OPENAI_TEMPERATURE; defaults to 0.7
  • OPENAI_TOP_P; defaults to 1
  • OPENAI_FREQUENCY_PENALTY; defaults to 0
  • OPENAI_PRESENCE_PENALTY; defaults to 0

About

Interface to ChatGPT from R

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • R 100.0%