The edit_code function prompts the user to provide a plain English
description of how the code in a given file should be modified. The function
then generates the modified code based on the user's input and writes it back
to the specified file. Only code is written: the code block of the reply, or
a reply without one that is entirely R code. If neither is returned (e.g. code
lines mixed with prose), the file is left unchanged.
Arguments
- filename
A character string representing the name of the file containing the code to be edited.
- chatlog_id
An optional character string, the ID of the chatlog that keeps the conversation history. The default,
NULL, uses one session per file, keyed by the file's normalized path, so other files are never sent along, and a session started bywrite_code()on the same file continues.- ...
Additional arguments to pass to the chat_completion() function.
Details
edit_code() is interactive: it reads the instructions from the console, and
in a non-interactive session (e.g. Rscript) it stops at once with an error
of class openair_not_interactive. In RStudio the file is shown in the
editor; elsewhere it is only rewritten. The instructions and the reply are
stored in the session only once a complete reply has arrived.
Examples
if (FALSE) { # \dontrun{
# Edit code in an existing file based on user input
f <- tempfile(fileext = ".R")
writeLines("x <- c(3, 1, 2)", f)
modified_code_file <- edit_code(f)
# Check the content of the modified code file
cat(readLines(modified_code_file), sep = "\n")
# Clean up
unlink(f)
} # }
