The write_code function prompts the user to provide a plain English
description of a program or function and the programming language it should
be written in. The function then generates the code based on the user's input
and writes it to a specified file.
Arguments
- filename
A character string representing the name of the file where the generated code will be saved.
- 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, soedit_code()on the same file continues the conversation and other files are never sent along.- ...
Additional arguments to pass to the chat_completion() function.
- overwrite
Logical. If
FALSE(default) andfilenamealready exists, the function stops before doing anything else (no prompt, no API call, and the file is left untouched). IfTRUE, an existing file is replaced, but only once the code has been generated.
Details
write_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 written.
The file is created (or replaced) only after the API returned code: a code block, or a reply without one that is entirely R code. If the call fails or no code is returned, nothing is written; the explanatory text around a code block is never written to the file. The instructions and the reply are stored in the session only once a complete reply has arrived.
