RClient Example
This graph shows the different features of the RClient operator.
This text assumes the reader has already read the R Client Operator documentation.
api$setSwitchCallback
switchFunc <- function(data) { pos <- FALSE if (data > 10) { pos <- TRUE } list(switchPosition=pos, output2=toString(data)) } api$setSwitchCallback(c("input5"), c("output2"), "switchFunc")
The argument data2 from input5 is of type int64. Every time inport input5 has data waiting to be read the switchFunc is called with this data as argument. The switch position will remain in its "off" (FALSE) state until the incoming data is greater than 10. When data is greater than 10, pos will be set to TRUE and the switch position will moved to its "on" state.
Notice that the switchPosition parameter name in the returned list should always be used in a switch callback function.
As explained in the RClient documentation, the switch "on" position enables all other callbacks associated with other ports. So this means that the other callbacks will start reading its input ports only when the inport input5 receives a value greater than 10.
api$setPortCallback
foo <- function(data1, data2) { list(output1=data1 + data2) } api$setPortCallback(c("input1", "input2"), c("output1"), "foo") bar <- function(data1, data2) { flag <- (data1*data2) %% (data1 + data2) == 0 if (!is.na(flag) && flag) { print("something") # Will only be printed in the Rserve stdout. return(NULL) # skip send to outports in this call } print("another thing") list(output1=data1 * data2, output2=toString(data1 + data2)) } api$setPortCallback(c("input1", "input2"), c("output1", "output2"), "bar")
However, the foo sends to outport output1 and bar sends to outports output1 and output2. As bar is registered after foo, when data is available in port input1 and input2 the same pair of data will first be used to call foo and then used to call bar.
-
You can use print("text") to send data to Rserve stdout.
-
If you return(NULL) then nothing will be sent to the output ports associated with this callback for this given call. This is useful if for some calls of the callback you don't want to send anything to the output ports associated with it.
Notice that we send a number to the output1 port as the port type is int64, and we send a string to output2 as the port type is string.
foo2 <- function(data1) { list(output2=paste("concat!", data1)) } api$setPortCallback(c("input3"), c("output2"), "foo2")
dataFrameFunc <- function(data) { data[1,2] <- data[1,2] * 10 list(output2=class(data), output3=data) } api$setPortCallback(c("input4"), c("output2", "output3"), "dataFrameFunc")
msg = api.Message([[u"abc", u"defg"], [counter, 10]], {u"fieldNames": [u"column 1", u"column 2"]}, u"table") api.send("output", msg) counter += 1
counter <<- 0L foo3 <- function(data1) { counter <<- counter + nchar(data1) } api$setPortCallback(c("input3"), c(), "foo3")
api$addTimer
periodicFunc1 <- function() { counter <<- counter + 1L list(output1=counter) } api$addTimer("1s", c("output1"), "periodicFunc1") periodicFunc2 <- function() { counter <<- counter * 2L } api$addTimer("2s", c(), "periodicFunc2")
The first increments the counter global variable and sends it to output1 port, and the second is not associated with any port, and just multiply the counter by 2.
api$addShutdownHandler
There are two shutdown callbacks in the RClient script. Those two will be called in the order they appear below when the graph is scheduled to stop or goes to dead.