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.
This is intended to be subclassed.
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.