-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaddRoxygenDescription.R
101 lines (87 loc) · 4.37 KB
/
addRoxygenDescription.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#' Add Roxygen Description to R Function
#'
#' @title Add Roxygen Description to R Function
#' @description This function adds a Roxygen description to an R function using the GPT-4 model.
#' It can either take the selected code from RStudio or read from the clipboard.
#' @param Model A character string specifying the GPT model to be used. Default is "gpt-4o-mini".
#' @param SelectedCode A logical value indicating whether to use the selected code in RStudio. Default is TRUE.
#' @param verbose Logical flag to indicate whether to display the generated text. Default is TRUE.
#' @importFrom assertthat assert_that is.string noNA
#' @importFrom clipr read_clip write_clip
#' @importFrom rstudioapi isAvailable getActiveDocumentContext insertText
#' @return A message indicating completion if `SelectedCode` is TRUE, otherwise the Roxygen-annotated code is copied to the clipboard.
#' @export addRoxygenDescription
#' @author Satoshi Kume
#' @examples
#' \dontrun{
#' # Option 1
#' # Select some text in RStudio and then run the rstudio addins
#' # Option 2
#' # Copy the text into your clipboard then execute
#' addRoxygenDescription(Model = "gpt-4o-mini", SelectedCode = FALSE)
#' }
addRoxygenDescription <- function(Model = "gpt-4o-mini",
SelectedCode = TRUE,
verbose = TRUE) {
# Get input either from RStudio or clipboard
if(SelectedCode){
assertthat::assert_that(rstudioapi::isAvailable())
input <- rstudioapi::getActiveDocumentContext()$selection[[1]]$text
} else {
input <- paste0(clipr::read_clip(), collapse = " \n")
}
if(verbose){
cat("\n", "addRoxygenDescription: ", "\n")
pb <- utils::txtProgressBar(min = 0, max = 3, style = 3)
}
# Assertions for input validation
assertthat::assert_that(
assertthat::is.string(input),
assertthat::noNA(input),
assertthat::is.string(Model),
Sys.getenv("OPENAI_API_KEY") != ""
)
# Initialize temperature for text generation
temperature <- 1
if(verbose){utils::setTxtProgressBar(pb, 1)}
# Template for text generation
template <- "
You are an excellent assistant and a highly skilled genius copilot of the R language.
You always have great answers about the R language.
You should understand the input R code carefully, line by line.
You create a Roxygen skeleton in English from the R function input.
You should output only the roxygen format strictly according to roxygen policy in English, without using code blocks.
"
# Create the prompt
template1 <- "
You need to output in roxygen2 format, using #' not only ' style in English.
Your definition should be in the roxygen2 format and include specific elements such as @title, @description, @param, @importFrom, @return, @export, @author [Blank], and @examples.
It is important to make sure that the function name you define is also defined in @export.
In addition, you should translate any comments in the function to English and make sure that the content of the comments is consistent and correct throughout the function.
If you find any errors or areas for improvement in the R script, you should correct them according to these instructions.
If there is a risk of errors occurring during text generation, you should make the necessary changes to prevent these errors.
Once you have completed the definition, please submit it as a final product.
You should use the assertthat::is.string, assertthat::is.count, assertthat::noNA, and other function groups in the assertthat package, and append the data verification execution command to the function according to the type of argument.
You should omit the R code output.:
"
template1s <- paste0(template1, paste0(input, collapse = " "), sep=" ")
# Initialize history for chat
history <- list(list('role' = 'system', 'content' = template),
list('role' = 'user', 'content' = template1s))
if(verbose){utils::setTxtProgressBar(pb, 2)}
# Execute text generation
res <- as.character(chat4R_history(history = history,
Model = Model,
temperature = temperature))
if(verbose){
utils::setTxtProgressBar(pb, 3)
cat("\n")}
# Combine Roxygen comments with original code
result <- paste0(res, "\n", input)
# Output the enriched text
if(SelectedCode){
rstudioapi::insertText(text = as.character(result))
} else {
return(clipr::write_clip(result))
}
}