JsonRPCObject: Base class to subclass to make a JsonRPCObject.

Description Details Examples

Description

A JsonRPCObject can expose methods that may be called by name by a client. See the function json_rpc for such a call. There needs to be the object name, the method name, any parameters (passed as a JavaScript object) and a callback which is run when the result is a success. See json_rpc_server for an example.

Details

This is intended to be subclassed.

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
## a subclass to return a data set
 DataSets <- setRefClass("DataSets",
                          contains="JsonRPCObject",
                          fields=list(
                            ctr="list"
                            ),
                          methods=list(
                            initialize=function(...) {
                              ctr <<- list()
                              to_export <- c("get_data") # exported methods
                              callSuper(to_export, ...)
                            },
                            get_data = function(name, column_mapping=list(), ...) {
                              "return data frame, possibly changing column names"
                              x <- get(name)
                              if(!is(x, "data.frame"))
                                stop("trying to access a non-data frame")

                              nms <- names(x)
                              for(i in names(column_mapping)) {
                                if(!is.na(ind <- match(i, nms)))
                                  names(x)[ind] <- column_mapping[[i]]
                              }
                              toJSON(list(data=x))
                            }
                            ))$new()
## Call with something like this:
w <- gwindow("JSON-RPC test")
## Now add json_rpc call to javascript queue
cmd <- "
json_rpc('DataSets', 'get_data',{name: 'morley', column_mapping:{'Expt':'Experiment'}},
function(response) {
  value = Ext.JSON.decode(response.responseText);
  alert('There are ' + value.data.Run.length + ' observations.');
});
"
## Add the JavaScript command to the queue so that the browser will
## get it when the queue is flushed
w$add_js_queue(cmd)

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