Generates 'TypeScript' schema for the given R function or file path. If a path, the R app is also generated.
Arguments
- f
A function or file path (length-one character string for file compilation).
- ...
Additional arguments. For the file path method, named arguments passed to
ts_deploy()(e.g.init,port,run). Forts_function()/ts_widgetobjects,formatandprettier_cmdare supported (see details); other arguments are ignored.
Value
For a ts_function() / ts_widget, a character string of 'TypeScript'.
For a file path, writes .ts / .R beside filename and returns
the output base path invisibly.
Details
ts_function() method: name defaults to deparse(substitute(f)) and sets the generated export const symbol.
Character (file) method: filename is the base path for output; .R and .ts
extensions are appended. When omitted, output goes under the default compile
directory (see below) as {basename(f)}.rserve.
Arguments filename, format, and prettier_cmd must be passed by name;
they are not part of ....
Default output directory (CRAN-safe; does not write beside the source by default):
option
RserveTS.compile_dirif set to a non-empty path;else environment variable
RSERVETS_COMPILE_DIRif set;else
tempdir().
For local development, set e.g. options(RserveTS.compile_dir = ".") or
RSERVETS_COMPILE_DIR=. so ts_compile("app.R") writes app.rserve.ts /
app.rserve.R in the working directory; or pass filename explicitly.
format– IfTRUE, run 'Prettier' (or a compatible CLI) on the generated 'TypeScript' (returned string for functions; written.tsfor files). Defaults togetOption("RserveTS.format", FALSE)so you can enable it once for a session (e.g. 'pkgdown' site builds) without changing call sites.prettier_cmd– Character vector argv (executable first). Defaults togetOption("RserveTS.prettier_cmd")(NULLuntil you set it, e.g.options(RserveTS.prettier_cmd = c("prettier", "--parser", "typescript"))). When stillNULL, falls back to environment variableRserveTS_PRETTIER_CMD(space-separated tokens), thenprettierornpx prettieronPATH. A temporary.tscopy of the generated source is appended as the last argument (as withprettier --parser typescript path/to/file.ts). Another formatter (e.g. 'Biome') can be used if it accepts that invocation pattern.
Examples
# Compile a typed function to a 'TypeScript' schema string (no files written)
f <- ts_function(function(x = ts_integer(1)) x + 1L, result = ts_integer(1))
ts_compile(f)
#> export const f = Robj.ocap([z.number()], Robj.integer(1));
# File compilation writes under tempdir() by default (or RSERVETS_COMPILE_DIR)
src <- tempfile(fileext = ".R")
writeLines(
"add <- ts_function(function(x = ts_integer(1)) x + 1L, result = ts_integer(1), export = TRUE)",
src
)
out <- ts_compile(src)
file.exists(paste0(out, ".ts"))
#> [1] TRUE
file.exists(paste0(out, ".R"))
#> [1] TRUE