(\"#hist\")!; histDiv.innerHTML = ` ${Array.from(hist.y) .map( (yi) => `<\/div>` ) .join(\"\")} `; }); // style.css :root { font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; line-height: 1.5; font-weight: 400; color-scheme: light dark; color: rgba(255, 255, 255, 0.87); background-color: #242424; font-synthesis: none; text-rendering: optimizeLegibility; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } a { font-weight: 500; color: #646cff; text-decoration: inherit; } a:hover { color: #535bf2; } body { margin: 0; display: flex; place-items: center; min-width: 320px; min-height: 100vh; } h1 { font-size: 3.2em; line-height: 1.1; } #app { max-width: 1280px; margin: 0 auto; padding: 2rem; text-align: center; } .logo { height: 6em; padding: 1.5em; will-change: filter; transition: filter 300ms; } .logo:hover { filter: drop-shadow(0 0 2em #646cffaa); } .logo.vanilla:hover { filter: drop-shadow(0 0 2em #3178c6aa); } .card { padding: 2em; } .read-the-docs { color: #888; } button { border-radius: 8px; border: 1px solid transparent; padding: 0.6em 1.2em; font-size: 1em; font-weight: 500; font-family: inherit; background-color: #1a1a1a; cursor: pointer; transition: border-color 0.25s; } button:hover { border-color: #646cff; } button:focus, button:focus-visible { outline: 4px auto -webkit-focus-ring-color; } input { border-radius: 8px; border: 1px solid transparent; padding: 0.6em 1.2em; font-size: 1em; font-weight: 500; font-family: inherit; background-color: #1a1a1a; color: #fff; outline: none; } input:hover { border-color: #646cff; } input:focus, input:focus-visible { outline: 4px auto -webkit-focus-ring-color; } @media (prefers-color-scheme: light) { :root { color: #213547; background-color: #ffffff; } a:hover { color: #747bff; } button { background-color: #f9f9f9; } input { background-color: #f9f9f9; color: #000; } }"},{"path":"http://tomelliott.co.nz/ts/articles/simple-react-app.html","id":"run-the-app","dir":"Articles","previous_headings":"","what":"Run the app","title":"Build a simple ReactJS app","text":"run app, start Rserve server: start Vite server: now able see app running http://localhost:5173 (similar, see console output).","code":"Rscript src/faithful-app.rserve.R pnpm run dev"},{"path":"http://tomelliott.co.nz/ts/authors.html","id":null,"dir":"","previous_headings":"","what":"Authors","title":"Authors and Citation","text":"Tom Elliott. Author, maintainer.","code":""},{"path":"http://tomelliott.co.nz/ts/authors.html","id":"citation","dir":"","previous_headings":"","what":"Citation","title":"Authors and Citation","text":"Elliott T (2025). ts: Helper functions writing type safe functions rserve-ts. R package version 0.0.0.9000, http://tomelliott.co.nz/ts/.","code":"@Manual{, title = {ts: Helper functions for writing type safe functions for rserve-ts}, author = {Tom Elliott}, year = {2025}, note = {R package version 0.0.0.9000}, url = {http://tomelliott.co.nz/ts/}, }"},{"path":"http://tomelliott.co.nz/ts/index.html","id":"ts-write-apps-with-r-rserve-and-typescript","dir":"","previous_headings":"","what":"Helper functions for writing type safe functions for rserve-ts","title":"Helper functions for writing type safe functions for rserve-ts","text":"ts package makes easy users write functions can used rserve-ts applications.","code":""},{"path":"http://tomelliott.co.nz/ts/index.html","id":"installation","dir":"","previous_headings":"","what":"Installation","title":"Helper functions for writing type safe functions for rserve-ts","text":"can install development version ts GitHub :","code":"# install.packages(\"devtools\") devtools::install_github(\"tmelliott/ts\")"},{"path":"http://tomelliott.co.nz/ts/index.html","id":"example","dir":"","previous_headings":"","what":"Example","title":"Helper functions for writing type safe functions for rserve-ts","text":"Writing functions easy, just use ts_*() functions define formals return types. use ts_compile() generate TypeScript schemas: can import rserve-ts application. See tests/testthat/sampler example. also possible generate sourceable file deploy Rserve instance app code using ts_deploy():","code":"# demo.R library(ts) addFn <- ts_function( function(a = ts_numeric(1), b = ts_numeric(1)) a + b, result = ts_numeric(1) ) sampleFn <- ts_function( function(x = ts_character(), n = ts_integer(1)) sample(x, n), result = ts_character() ) app <- ts_function( function() { list( add = addFn, sample = sampleFn ) }, result = ts_list( add = addFn, sample = sampleFn ) ) # TODO: specify exactly which functions to export in the entry point # ts_export(app) import { Robj } from 'rserve-ts'; import { z } from 'zod'; const addFn = Robj.ocap([z.number(), z.number()], Robj.numeric(1)); const app = Robj.ocap([], Robj.list({ add: Robj.ocap(), sample: Robj.ocap() }) ); const sampleFn = Robj.ocap( [z.union([z.string(), z.array(z.string())]), z.number()], Robj.character() ); export default { addFn, app, sampleFn }; ts_deploy(app) # run with: Rscript app.rserve.R"},{"path":"http://tomelliott.co.nz/ts/reference/ts_app.html","id":null,"dir":"Reference","previous_headings":"","what":"Generate an Rserve app from a ts function — ts_app","title":"Generate an Rserve app from a ts function — ts_app","text":"Anything function simply returns . However, functions wrapped Rserve::ocap(), result subsequently wrapped ts_app().","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_app.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Generate an Rserve app from a ts function — ts_app","text":"","code":"ts_app(x)"},{"path":"http://tomelliott.co.nz/ts/reference/ts_app.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Generate an Rserve app from a ts function — ts_app","text":"x ts function object (ts_function())","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_character.html","id":null,"dir":"Reference","previous_headings":"","what":"Character or string type — ts_character","title":"Character or string type — ts_character","text":"Strings represented Zod schema either string (z.string()), string array (z.array(z.string())).","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_character.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Character or string type — ts_character","text":"","code":"ts_character(n = -1L)"},{"path":"http://tomelliott.co.nz/ts/reference/ts_character.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Character or string type — ts_character","text":"n length string vector. n = 1 single string expected. n = 0 length expected. n > 1 string vector length n expected.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_character.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Character or string type — ts_character","text":"ts object accepts strings string vectors length n.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_compile.html","id":null,"dir":"Reference","previous_headings":"","what":"Compile R functions — ts_compile","title":"Compile R functions — ts_compile","text":"Generates TypeScript schema given R function file path. path, R app also generated.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_compile.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Compile R functions — ts_compile","text":"","code":"ts_compile(f, ..., name, filename)"},{"path":"http://tomelliott.co.nz/ts/reference/ts_compile.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Compile R functions — ts_compile","text":"f function file path ... Additional arguments (passed ts_deploy) name name function filename base file path write TypeScript schema R app (optional, uses [path f].rserve default). .R .ts file extensions appended automatically. \"\", output printed standard output console (see cat).","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_compile.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Compile R functions — ts_compile","text":"Character vector TypeScript schema, NULL writing file","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_dataframe.html","id":null,"dir":"Reference","previous_headings":"","what":"Typed dataframe — ts_dataframe","title":"Typed dataframe — ts_dataframe","text":"essentially list, elements must names length.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_dataframe.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Typed dataframe — ts_dataframe","text":"","code":"ts_dataframe(...)"},{"path":"http://tomelliott.co.nz/ts/reference/ts_dataframe.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Typed dataframe — ts_dataframe","text":"... Named types.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_dataframe.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Typed dataframe — ts_dataframe","text":"ts object accepts data frames specified types.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_deploy.html","id":null,"dir":"Reference","previous_headings":"","what":"Deploy a ts Rserve app — ts_deploy","title":"Deploy a ts Rserve app — ts_deploy","text":"Deploy ts Rserve app","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_deploy.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Deploy a ts Rserve app — ts_deploy","text":"","code":"ts_deploy( f, file = sprintf(\"%s.rserve.R\", tools::file_path_sans_ext(f)), init = NULL, port = 6311, run = c(\"no\", \"here\", \"background\"), silent = FALSE )"},{"path":"http://tomelliott.co.nz/ts/reference/ts_deploy.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Deploy a ts Rserve app — ts_deploy","text":"f path application files file file write deployment script init Names objects (ts_functions) make available initialisation function port port deploy app run Whether run deployment script, takes values \"\", \"\", \"background\" silent Whether print deployment script","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_deploy.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Deploy a ts Rserve app — ts_deploy","text":"NULL, called open Rserve instance","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_factor.html","id":null,"dir":"Reference","previous_headings":"","what":"Typed factor — ts_factor","title":"Typed factor — ts_factor","text":"Factors integers labels. JS side, always represented string array (even one value - yay!).","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_factor.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Typed factor — ts_factor","text":"","code":"ts_factor(levels = NULL)"},{"path":"http://tomelliott.co.nz/ts/reference/ts_factor.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Typed factor — ts_factor","text":"levels character vector levels (optional).","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_factor.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Typed factor — ts_factor","text":"ts object accepts factors specified levels.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_function.html","id":null,"dir":"Reference","previous_headings":"","what":"TS function definition — ts_function","title":"TS function definition — ts_function","text":"TS function definition","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_function.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"TS function definition — ts_function","text":"","code":"ts_function(f, ..., result = ts_void())"},{"path":"http://tomelliott.co.nz/ts/reference/ts_function.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"TS function definition — ts_function","text":"f R function ... argument definitions (required f specify formals) result return type (ignored overloads provided)","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_function.html","id":"details","dir":"Reference","previous_headings":"","what":"Details","title":"TS function definition — ts_function","text":"Defining functions core writing Rserve apps. Functions referred object capabilities (ocaps), 'objects' allow Javascript access capabilities R restricted interface. arguments can adjusted. TS functions can defined using existing (named) anonymous functions. Anonymous functions useful arguments functions can explicitly defined types formal arguments:","code":"ts_function(function(x = ts_integer(), y = ts_string()) { ... })"},{"path":"http://tomelliott.co.nz/ts/reference/ts_integer.html","id":null,"dir":"Reference","previous_headings":"","what":"Integer type — ts_integer","title":"Integer type — ts_integer","text":"Integers represented Zod schema either number (z.number()), Int32Array (z.instanceof(Int32Array)).","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_integer.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Integer type — ts_integer","text":"","code":"ts_integer(n = -1L)"},{"path":"http://tomelliott.co.nz/ts/reference/ts_integer.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Integer type — ts_integer","text":"n length integer vector. n = 1 single integer expected. n = 0 length expected. n > 1 integer vector length n expected.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_integer.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Integer type — ts_integer","text":"ts object accepts integer scalars vectors length n.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_list.html","id":null,"dir":"Reference","previous_headings":"","what":"Typed list — ts_list","title":"Typed list — ts_list","text":"list vector robjects, may may named.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_list.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Typed list — ts_list","text":"","code":"ts_list(...)"},{"path":"http://tomelliott.co.nz/ts/reference/ts_list.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Typed list — ts_list","text":"... list types, named unnamed.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_list.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Typed list — ts_list","text":"ts object accepts lists specified types.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_logical.html","id":null,"dir":"Reference","previous_headings":"","what":"Logical or boolean type — ts_logical","title":"Logical or boolean type — ts_logical","text":"Booleans represented Zod schema either boolean (z.boolean()), typed Uint8Array (z.instanceof(Uint8Array)).","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_logical.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Logical or boolean type — ts_logical","text":"","code":"ts_logical(n = -1L)"},{"path":"http://tomelliott.co.nz/ts/reference/ts_logical.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Logical or boolean type — ts_logical","text":"n length boolean vector. n = 1 single boolean expected. n = 0 length expected. n > 1 boolean vector length n expected.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_logical.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Logical or boolean type — ts_logical","text":"ts object accepts logical scalars vectors length n.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_null.html","id":null,"dir":"Reference","previous_headings":"","what":"Null type — ts_null","title":"Null type — ts_null","text":"type accepts NULL.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_null.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Null type — ts_null","text":"","code":"ts_null()"},{"path":"http://tomelliott.co.nz/ts/reference/ts_null.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Null type — ts_null","text":"ts object accepts NULL.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_numeric.html","id":null,"dir":"Reference","previous_headings":"","what":"Numeric type — ts_numeric","title":"Numeric type — ts_numeric","text":"Numbers represented Zod schema either number (z.number()), Float64Array (z.instanceof(Float64Array)).","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_numeric.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Numeric type — ts_numeric","text":"","code":"ts_numeric(n = -1L)"},{"path":"http://tomelliott.co.nz/ts/reference/ts_numeric.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Numeric type — ts_numeric","text":"n length numeric vector. n = 1 single number expected. n = 0 length expected. n > 1 numeric vector length n expected.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_numeric.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Numeric type — ts_numeric","text":"ts object accepts numeric scalars vectors length n.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_object.html","id":null,"dir":"Reference","previous_headings":"","what":"Typed object — ts_object","title":"Typed object — ts_object","text":"base type typed objects, can used define custom types.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_object.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Typed object — ts_object","text":"","code":"ts_object( input_type = \"any\", return_type = \"any\", default = NULL, check = function() stop(\"Not implemented\"), generic = FALSE ) is_ts_object(x) get_type(x, which = c(\"input\", \"return\")) check_type(type, x)"},{"path":"http://tomelliott.co.nz/ts/reference/ts_object.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Typed object — ts_object","text":"input_type type object Typescript expect send R. return_type type object Typescript expects recieve R. default default value object. check function checks object returns valid. operates R side mostly development debugging purposes. developer ensure functions return correct type object always. generic logical, TRUE object generic type. x object type get, either \"input\" \"return\" type ts object","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_object.html","id":"functions","dir":"Reference","previous_headings":"","what":"Functions","title":"Typed object — ts_object","text":"is_ts_object(): Check object ts object get_type(): Get input type ts object check_type(): Check object correct type","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_union.html","id":null,"dir":"Reference","previous_headings":"","what":"Union type — ts_union","title":"Union type — ts_union","text":"Create union types. Currently accepts schemas strings.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_union.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Union type — ts_union","text":"","code":"ts_union(...)"},{"path":"http://tomelliott.co.nz/ts/reference/ts_union.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Union type — ts_union","text":"... Types merge","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_void.html","id":null,"dir":"Reference","previous_headings":"","what":"Void type — ts_void","title":"Void type — ts_void","text":"type accepts null values (typically used functions return nothing).","code":""},{"path":"http://tomelliott.co.nz/ts/reference/ts_void.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Void type — ts_void","text":"","code":"ts_void()"},{"path":"http://tomelliott.co.nz/ts/reference/ts_void.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Void type — ts_void","text":"ts object accepts NULL.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/type_objects.html","id":null,"dir":"Reference","previous_headings":"","what":"Types in R and TypeScript — type_objects","title":"Types in R and TypeScript — type_objects","text":"document provides overview main types available app developers, includes main types R TypeScript counterparts.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/type_objects.html","id":"ts-objects","dir":"Reference","previous_headings":"","what":"TS objects","title":"Types in R and TypeScript — type_objects","text":"basic object ts ts_object class. represented input return types, optional default value, checking function. Input types describe zod schema objects TypeScript can pass Rserve functions. Return types describe zod schema objects Rserve functions, utilise Robj utility types rserve-ts library. return types additional properties added, namedly r_type r_attributes, handled Robj utility types. Scalar versus array (\"vector\") types: R, almost types vectors. 'rserve-js' library, primitive arrays length one converted scalars, leads issues type checking , example, return value unknown length. (x > 5) one example. solve , add n argument ts_* functions. n = 1, type takes scalar form alue. n != 1, type takes array form value (includes 0). Otherwise, type union scalar array forms. case numbers, strings, booleans.","code":""},{"path":"http://tomelliott.co.nz/ts/reference/type_objects.html","id":"available-types","dir":"Reference","previous_headings":"","what":"Available types","title":"Types in R and TypeScript — type_objects","text":"ts_boolean: boolean value. array type Int8Array. ts_integer: integer value. array type Int32Array. Javascript native integer type, scalars represented number (ts_numeric). ts_numeric: numeric value. array type Float64Array.* ts_string: string value. array type string[]. ts_factor: factor value. array type (level1 | level2 | ... | levelN)[], type scalar form. ts_list: list value, represented named object array. ts_dataframe: data frame value, represented named object. ts_null: null value. ts_void: void value, used specifying return types functions return value.","code":""}]