File: callback.R

package info (click to toggle)
r-cran-v8 6.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 436 kB
  • sloc: javascript: 980; cpp: 424; sh: 23; makefile: 8
file content (52 lines) | stat: -rw-r--r-- 1,451 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Internal function used for the JavaScript console.r API
#
# Provides: console.r.call("rnorm", {n:10})
r_call <- function(strfun, args = '{}'){
  no_jumps({
    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 = '{}'){
  no_jumps({
    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}'){
  no_jumps({
    con <- textConnection(str)
    ARGS <- as.list(jsonlite::fromJSON(args))
    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 = '{}'){
  no_jumps({
    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')
  })
}

no_jumps <- function(...){
  tryCatch(..., error = function(e){
    structure(e$message, class = 'cb_error')
  }, interrupt = function(e){
    structure("User interruption during R evaluation", class = 'cb_error')
  })
}