json_rpc_server: Start a json RPC server

Description Usage Arguments Details See Also Examples

View source: R/json-rpc.R

Description

This JSON RPC server allows the JavaScript programmer to call back into R, specifying an object by name (whose class is a subclass of JsonRPCObject), a method name (in the exported methods of the object) and a JSON encoded set of parameters. The server will return the results of the method call as a JSON encoded object. The example uses Ext.Ajax.request to make the request and Ext.json.decode to decode the response. To use this successfully, one needs to write a callback in JavaScript to do something with the returned value.

Usage

1
json_rpc_server(url = "JSON_RPC", port = 9000, envir = .GlobalEnv)

Arguments

url

URL to run server under. Defaults to "JSON_RPC", so url would be (without work), "/custom/JSON_RPC"

port

Which port to start Rhttpd, if Rhttpd server has not already started.

envir

Where to look for JsonRPCObjects, defaults to global environment

Details

The basic idea is one starts the server, then creates subclasses of the JsonRPCObject class. See the example for how this can be done.

We memoise this call, so that the server is only started one time, though one could bypass this with the load reference method (say one wanted multiple urls for the server).

See Also

JsonRPCObject

Examples

 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
## Not run: 
## Create a PageCounter object using JSON_RPC
w <- gwindow("PageCounter")
g <- ggroup(cont=w)
page_ctr <- glabel("replace me below...", cont=g)


## Run this once
## It sets up the PageCounter JsonRPCObject and starts the server
where <- .GlobalEnv ## where to look for objects
if(!exists("PageCounter", where)) {
  json_rpc_server(envir=where)
  PageCounter <- setRefClass("PageCounter",
                             contains="JsonRPCObject",
                             fields=list(
                               ctr="list"
                               ),
                             methods=list(
                               initialize=function(...) {
                                 ctr <<- list()
                                 to_export <- c("value") # exported methods
                                 callSuper(to_export, ...)
                               },
                               get_count = function(page, ...) {
                                 ## could do something with a file here
                                 ## should do file locking though!
                                 ## f <- "/tmp/ctr.txt"
                                 ## if(file.exists(f)) {
                                 ##   ctr <- read.table(f,
                                 ##                     colClasses=c("character", "integer"),
                                 ##                     header=TRUE)
                                 ## } else {
                                 ##   ctr <- data.frame(page=character(0), count=integer(0),
                                 ##                     stringsAsFactors=FALSE)
                                 ## }
                                 ## if(!is.na(ind <- match(page,ctr[,1]))) {
                                 ##   count <- ctr[ind,2] + 1
                                 ##   ctr[ind,2] <- count
                                 ## } else {
                                 ##   count <- 1
                                 ##   ctr[nrow(ctr) + 1, ] <- list(page, count)
                                 ## }
                                 ## write.table(ctr, file=f, row.names=FALSE)
                                 return(count)


                                 if(is.null(ctr[[page]]))
                                   count <- 1
                                 else
                                   count <- ctr[[page]] + 1
                                 ctr[[page]] <<- count
                                 return(count)
                               },
                               value=function(page, ...) {
                                 val <- sprintf("Page accessed %s times", get_count(page))
                                 print(val)
                                 toJSON(list(text=val))
                               }
                             ))$new()
  ## JsonRPCObjects are found in the global environment by default
  assign("PageCounter", PageCounter, where)
}

## Now add json_rpc call to javascript queue setText method for a
## label widget. See the set_value reference method to know. The JavaScript function
## json_rpc is defined in the \code{load_AJAX.rhtml} file.
tpl <- "
json_rpc('PageCounter', 'value',{page: '{{app_name}}' },
function(response) {
  txt = Ext.JSON.decode(response.responseText);
  {{id}}.setText(txt.text);
});
"
cmd <- whisker.render(tpl, list(id=page_ctr$get_id(),
                                app_name="page_counter_example"))

## Add the JavaScript command to the queue so that the browser will
## get it when the queue is flushed
w$add_js_queue(cmd)

## End(Not run)

jverzani/gWidgetsWWW2 documentation built on Feb. 9, 2020, 5:18 p.m.