This function takes R code (a file, a URL or a character string) as input and uses a language model to convert it to Python code. The converted Python code is then either returned as a character string or written to a file, depending on the input.
Arguments
- r
A file path, URL or character string containing the R code to be converted (see
read_text()).- ...
Additional arguments to pass to the chat_completion() function.
- overwrite
Logical. If
FALSE(default), the function stops before calling the API when the output file already exists; ifTRUE, it replaces it. Only used for file input.
Value
For literal text or a URL: the converted Python code as a character
string, returned visibly; nothing is written. For a file: a new Python file
is created with the same name as the input file but with a ".py" extension,
and its path is returned invisibly. For file input, output that does not
parse as Python is not written: the function stops with an error of class
openair_invalid_output whose output field holds the code. Literal or URL
input returns such output all the same, with a message that it may be
invalid. If no Python
interpreter is available to check it, the output is written unchecked (see
is_python()). Empty input stops with an error of class
openair_empty_input before any API call.
Examples
if (FALSE) { # \dontrun{
# Convert R code to Python code and return it as a character string
r_to_python("x <- c(1, 2, 3)\nmean(x)")
# Convert an R file in the temporary directory: the Python code is written
# to a .py file next to it, whose path is returned invisibly
f <- tempfile(fileext = ".R")
writeLines(c("x <- c(1, 2, 3)", "mean(x)"), f)
py_file <- r_to_python(f)
cat(readLines(py_file), sep = "\n")
unlink(c(f, py_file))
} # }
