File: callback.R

package info (click to toggle)
r-cran-v8 3.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 552 kB
  • sloc: javascript: 2,438; cpp: 1,054; sh: 10; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 1,126 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Internal function used for the JavaScript console.r API
#
# Provides: console.r.call("rnorm", {n:10})
r_call <- function(strfun, args = '{}'){
  FUN <- eval(parse(text=strfun))
  ARGS <- as.list(jsonlite::fromJSON(args))
  if(!is.function(FUN))
    stop("Argument is not a valid function")
  out <- do.call(FUN, ARGS)
  jsonlite::toJSON(out)
}

# Provides: console.r.get("iris")
r_get <- function(str, args = '{}'){
  x <- eval(parse(text = str))
  ARGS <- as.list(jsonlite::fromJSON(args))
  do.call(jsonlite::toJSON, c(list(x = x), ARGS))
}

# Provides: console.r.eval("rnorm(10)")
r_eval <- function(str, args = '{"print.eval":true}'){
  con <- textConnection(str)
  ARGS <- as.list(jsonlite::fromJSON(args))
  out <- do.call(source, c(list(file = con), ARGS))
  #tryCatch(toJSON(out$value), error = 'null')
  return('null')
}

# Provides: console.r.assign("test", [1,2,3])
r_assign <- function(name, value, args = '{}'){
  ARGS <- as.list(jsonlite::fromJSON(args))
  VAL <- do.call(jsonlite::fromJSON, c(list(txt = value), ARGS))
  asgn <- get("assign", "package:base")
  asgn(name, VAL, globalenv())
  return('null')
}