evalServer: Evaluate R code in a server process

Description Usage Arguments Details Value Author(s) See Also Examples

Description

This function is designed to connect two R processes together using the socket server. It allows for piloting the server R process from a client R process, to evaluate R code in the server and return its results to the client.

Usage

1
evalServer(con, expr, send = NULL)

Arguments

con

a socket connection with the server (see examples).

expr

an R expression to evaluate in the server.

send

optional data to send to the server.

txt_expr

an R string with a command line(s) to evaluate on the server. If R is a vector, it will get concatenated into one string, with each line infixed with semicolon (';').

Details

The function serializes R objects using dump() on the server, and it source()s the data on the client side. It has, thus, the same limitations as dump, (see ?dump), and in particular, environments, external pointers, weak references and objects of type S4 are not serializable with dump() and will raise an error, or will produce unusable objects on the client side. Note also that lists or attributes of accepted objects may contain external pointers or environments, and thus, the whole object becomes unserializable. In that case, try to coerce your object, or extract a part of it on the server side to make sure you send just the part that is transferable between the two R processes.

txt_expr is a convenience that discards non-standard evaluation. It is usefull when you want to write a wrapper function to evalServer.

Value

The object returned by the last evaluation in the server.

Author(s)

Matthew Dowle (mdowle@mdowle.plus.com)

See Also

sendSocketClients

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
## Not run: 
## Start an R process and make it a server
require(svSocket)
startSocketServer()

## Start a second R process and run this code in it (the R client):
require(svSocket)

## Connect with the R socket server
con <- socketConnection(host = "localhost", port = 8888, blocking = FALSE)

L <- 10:20
L
evalServer(con, L)             # L is not an the server, hence the error
evalServer(con, L, L)          # Send it to the server
evalServer(con, L)             # Now it is there
evalServer(con, L, L + 2)
L
evalServer(con, L)

## More examples
evalServer(con, "x <- 42")     # Set x
evalServer(con, "y <- 10")     # Set y
evalServer(con, x + y)         # Don't need quotes
evalServer(con, "x + y")       # but you can put quotes if you like
evalServer(con, x)             # Same as get x
evalServer(con, "x + Y")       # Return server side-error to the client
evalServer(con, x)             # Keep working after an error
evalServer(con, "x <- 'a'")    # Embedded quotes are OK

## Examples of sending data
evalServer(con, X, -42)        # Alternative way to assign to X
evalServer(con, Y, 1:10)
evalServer(con, X + Y)
X  # Generates an error, X is not here in the client, only on the server
evalServer(con, X)
evalServer(con, "Z <- X + 3")  # Send an assignment to execute remotely
evalServer(con, X + Z)
evalServer(con, "Z <- X + 1:1000; NULL")   # Same but prevents Y being returned
evalServer(con, length(Z))
Z <- evalServer(con, Z)        # Bring it back to client
Z

## Examples with txt_eval
customsend<-function(con, code)
{
  evalServer(con,eval=NULL,txt_eval=code) # This might be much more fancy function burried deep in your code
}
code<-"y<-43"
customsend(con,code)           # sets y<-43
code<-c("y<-43", "x<-'string'")
customsend(con,code)           # sets y<-43 and x<-'string' on server. Equivalent to...
code<-"y<-43;x<-'string'"
customsend(con,code)           # ...this example.



## Close connection with the R socket server
close(con)

## Now, switch back to the R server process and check
## that the created variables are there
L
x
y
X
Y
Z

## Stop the socket server
stopSocketServer()

## End(Not run)

adamryczkowski/svSocket documentation built on May 10, 2019, 5:51 a.m.