Skip to contents

Checks a document of a call as r8check_call() does, raises every problem in one r8_argument_error, and otherwise calls the function that "$function" names with the arguments in their stored form. An argument that the document omits takes its default, and null is NULL. The call is evaluated in an environment that binds the arguments by name, whose parent is the frame of the caller of r8call_from_json(): sys.call() in the function's body shows the arguments' names, and parent.frame() is that environment.

Usage

r8call_from_json(f, json, load = NULL)

Arguments

f

A typed function of new_function(), or a list of them: the functions that the document may call. The document never selects other code.

json

Character: The document, one string or its lines.

load

Optional Character: Packages to load before the check, as r8from_json() takes them.

Value

The function's result.

Examples

op <- options(r8.namespace = "demo")
clip <- new_function(
  "clip",
  function(x = arg_vec(type_double()), lo = arg_double(default = 0), hi = arg_double()) {
    pmin(pmax(x, lo), hi)
  }
)
r8call_from_json(clip, '{"$function": "demo::clip", "arguments": {"x": [-1, 0.5, 3], "hi": 1}}')
#> [1] 0.0 0.5 1.0
options(op)