ts/R/compile.R

69 lines
1.9 KiB
R

#' Compile R functions
#'
#' Generates TypeScript schema for the given R function or file path. If a path, the R app is also generated.
#'
#' @param f A function or file path
#' @param name The name of the function
#' @param ... Additional arguments (passed to ts_deploy)
#' @param filename The base file path to write the TypeScript schema and R app to (optional, uses `[path of f].rserve` by default). `.R` and `.ts` file extensions are appended automatically. If `""`, the output is printed to the standard output console (see `cat`).
#' @return Character vector of TypeScript schema, or NULL if writing to file
#' @md
#' @export
ts_compile <- function(f, ..., name, filename) {
o <- UseMethod("ts_compile")
}
#' @export
ts_compile.ts_function <- function(f, ..., name = deparse(substitute(f))) {
inputs <- f$args
result <- f$result
inputs <- sapply(inputs, \(x) x$input_type)
fn_args <- paste(paste(inputs), collapse = ", ")
sprintf(
"const %s = Robj.ocap([%s], %s);", name, fn_args,
result$return_type
)
}
#' @export
ts_compile.character <- function(
f,
...,
filename = sprintf("%s.rserve", tools::file_path_sans_ext(f))) {
if (length(f) > 1) {
return(sapply(f, ts_compile))
}
if (!file.exists(f)) {
warning(sprintf("File not found: %s", f))
return()
}
e <- new.env()
source(f, local = e)
x <- sapply(ls(e), \(x) ts_compile(e[[x]], filename = "", name = x))
src <- c(
"import { Robj } from 'rserve-ts';",
"import { z } from 'zod';",
"\n",
x,
"\n",
sprintf("export default {\n %s\n};", paste(ls(e), collapse = ",\n "))
)
cat(src, file = sprintf("%s.ts", filename), sep = "\n")
# R file
ts_deploy(f, file = sprintf("%s.R", filename), silent = TRUE, ...)
invisible()
}
#' @export
ts_compile.default <- function(f, ...) {
warning("Not supported")
}