jcrodriguez1989 / chatgpt

Interface to ChatGPT from R
GNU General Public License v3.0
312 stars 37 forks source link

clipr for using the code copied to the clipboard #19

Closed denisabd closed 1 year ago

denisabd commented 1 year ago

Hello there - great package, I just thought about building something like this, but it is already done 👍

I think a good addition could be using the function code that is copied to the clipboard, similar to reprex package. User will select the code, Ctrl/Command + C and run any of the package functions.

It will require clipr as a dependency and one more enviroment variable that will use this "new" approach by default. Alternatively we can allow running functions without specified value for code argument and it will use the code from clipboard and stop() when the clipboard is NULL.

I just tried it and works quite well.

I can work on it and raise a PR if you're interested. Please let me know your thoughts.

Have a great day!

Denis

denisabd commented 1 year ago

I actually went ahead and created a fork Check it out and let me know if I can raise a PR for it. Here is one of the functions as an example:

comment_code <- function(code = NULL) {

  if (is.null(code)) {
    code <- read_clip(allow_non_interactive = TRUE)
  }

  if (is.null(code)) {
    stop("Add your code as an argument of copy code to clipboard")
  } else {
    code <- paste(str_replace_all(code, "\"", "'"), collapse = "\n")
  }

  prompt <- paste0('Add inline comments to the following R code: "', code, '"')
  parse_response(gpt_get_completions(prompt))
}

Added imports to every function as well

#' @importFrom clipr read_clip
#' @importFrom stringr str_replace_all

Thanks in advance!

jcrodriguez1989 commented 1 year ago

Hi @denisabd , thanks for proposing this idea! Just to be clear, this use-case will only be applied when running the function through the terminal. Right now, if we use the addins without selecting code, it will use all of the current file's code. But, yes, I love it. Could you create the PR, I have some comments for the code. First of all, could you replace str_replace_all(code, "\"", "'") by gsub('"', "'", code)? So we avoid importing {string}. Also, please remove the man/ folder and README.md from the PR, as these are auto-generated. Thanks!

jcrodriguez1989 commented 1 year ago

What about if instead of this kind of code:

comment_code <- function(code = NULL) {

  if (is.null(code)) {
    code <- read_clip(allow_non_interactive = TRUE)
  }

  if (is.null(code)) {
    stop("Add your code as an argument of copy code to clipboard")
  } else {
    code <- paste(str_replace_all(code, "\"", "'"), collapse = "\n")
  }

  prompt <- paste0('Add inline comments to the following R code: "', code, '"')
  parse_response(gpt_get_completions(prompt))
}

we do (to keep it as simple as possible):

comment_code <- function(code = clipr::read_clip(allow_non_interactive = TRUE)) {
  prompt <- paste0('Add inline comments to the following R code: "', code, '"')
  parse_response(gpt_get_completions(prompt))
}
denisabd commented 1 year ago

Will do in the next day or two. Do I raise a PR to develop branch?

jcrodriguez1989 commented 1 year ago

Yes, please. thanks!

jcrodriguez1989 commented 1 year ago

Fixed by https://github.com/jcrodriguez1989/chatgpt/pull/21 @denisabd , thanks for your contribution!!