[Sciviews-commits] r154 - in pkg/svSocket: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jul 16 06:48:53 CEST 2009


Author: phgrosjean
Date: 2009-07-16 06:48:52 +0200 (Thu, 16 Jul 2009)
New Revision: 154

Added:
   pkg/svSocket/R/evalServer.R
   pkg/svSocket/man/evalServer.Rd
Modified:
   pkg/svSocket/DESCRIPTION
   pkg/svSocket/NAMESPACE
   pkg/svSocket/NEWS
Log:
evalServer() added in svSocket

Modified: pkg/svSocket/DESCRIPTION
===================================================================
--- pkg/svSocket/DESCRIPTION	2009-07-15 13:38:10 UTC (rev 153)
+++ pkg/svSocket/DESCRIPTION	2009-07-16 04:48:52 UTC (rev 154)
@@ -3,7 +3,7 @@
 Depends: R (>= 2.6.0), tcltk, svMisc
 Description: Implements a simple socket server allowing to connect GUI clients to R
 Version: 0.9-44
-Date: 2009-05-30
+Date: 2009-07-15
 Author: Philippe Grosjean
 Maintainer: Philippe Grosjean <phgrosjean at sciviews.org>
 License: GPL (>= 2)

Modified: pkg/svSocket/NAMESPACE
===================================================================
--- pkg/svSocket/NAMESPACE	2009-07-15 13:38:10 UTC (rev 153)
+++ pkg/svSocket/NAMESPACE	2009-07-16 04:48:52 UTC (rev 154)
@@ -1,6 +1,7 @@
 import(tcltk)
 
-export(getSocketClients,
+export(evalServer,
+	   getSocketClients,
        getSocketClientsNames,
        getSocketServerName,
        getSocketServers,

Modified: pkg/svSocket/NEWS
===================================================================
--- pkg/svSocket/NEWS	2009-07-15 13:38:10 UTC (rev 153)
+++ pkg/svSocket/NEWS	2009-07-16 04:48:52 UTC (rev 154)
@@ -1,7 +1,13 @@
 = svSocket News
 
+== Changes in svSocket 0.9-44
+* Added function evalServer() for R interprocess communication using this R
+  socket server mechanism
+
+
 == Changes in svSocket 0.9-43
 * Example added in processSocket(), implementing a simple REPL
+
 * A new function, sendSocketServer() is added to send and evaluate commands from
   one R instance (client) to another one (a R socket server)
 

Added: pkg/svSocket/R/evalServer.R
===================================================================
--- pkg/svSocket/R/evalServer.R	                        (rev 0)
+++ pkg/svSocket/R/evalServer.R	2009-07-16 04:48:52 UTC (rev 154)
@@ -0,0 +1,41 @@
+evalServer <- function (con, expr, send = NULL) {
+	# Evaluate expr on the server, and return its value
+	# expr may be quoted or not e.g. can pass a quoted "x=2" to send an assignment
+	# send is optional. If supplied, expr must be a single object name (unquoted).
+	# Then send is evaluated on the client and the result is assigned to that
+	# object name on the server.
+	x <- substitute(expr)
+	if (!missing(send) && (!length(x) == 1 || mode(x) != "name"))
+		stop("When send is supplied, expr must be an target variable name (unquoted) on the server to assign the result of the send expr to.")
+	if (!is.character(x)) x <- deparse(x)
+	# Flush the input stream just in case previous calls failed to clean up
+	scan(con, "", quiet = TRUE)
+	if (missing(send)) {
+		writeLines(paste('.Last.value = try(eval(parse(text = "', x, '"))); dump(".Last.value", file = ""); cat("<<<endflag>>>")', sep = ""), con)
+	} else {
+		.Last.value <- send
+		sendtxtcon <- textConnection("tmptxt", open = "w", local = TRUE)
+		dump(".Last.value", sendtxtcon)
+		on.exit(close(sendtxtcon))
+		writeLines(paste(paste(tmptxt, collapse = ""), ';', x, ' = .Last.value; cat("<<<endflag>>>")', sep = ""), con)
+	}
+	# The explicit .Last.value is required to suppress the output of the value
+	# of expression back to the client e.g. "[1] 2" being sent back
+	# We use .Last.value as its a safe temporary variable to overwrite.
+	# The evaluate and dump must be sent as a single string
+	# using a single call to writeLines, to ensure multiple clients
+	# do not get mixed up results.
+	objdump <- ""
+	while (regexpr("<<<endflag>>>", objdump) < 0)
+		objdump <- paste(objdump, readLines(con), sep = "", collapse = "\n")
+	objdump <- sub("<<<endflag>>>", "", objdump)
+	if (!missing(send)) {
+		if (!identical(objdump, "")) stop(objdump)
+		return(TRUE)
+	}
+	# Source the content of objdump, locally in this frame
+	objcon <- textConnection(objdump)
+	on.exit(close(objcon))
+	source(objcon, local = TRUE, echo = FALSE, verbose = FALSE)
+	.Last.value
+}

Added: pkg/svSocket/man/evalServer.Rd
===================================================================
--- pkg/svSocket/man/evalServer.Rd	                        (rev 0)
+++ pkg/svSocket/man/evalServer.Rd	2009-07-16 04:48:52 UTC (rev 154)
@@ -0,0 +1,89 @@
+\name{evalServer}
+\alias{evalServer}
+
+\title{ Evaluate R code in a server process. }
+\description{
+  This function is designed to connect two R processes together using
+  the socket server. This function 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{
+evalServer(con, expr, send = NULL)
+}
+
+\arguments{
+  \item{con}{ A socket connection with the server (see examples) }
+  \item{expr}{ An R expression to evaluate in the server }
+  \item{send}{ Optional data to send to the server }
+}
+
+\value{
+  The object returned by the last evaluation in the server.
+}
+
+\author{Matthew Dowle (\email{m.dowle at wintoncapital.com})}
+
+\seealso{ \code{\link{sendSocketServer}} }
+
+\examples{
+\dontrun{
+# 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")
+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")       # returns server side error to the client
+evalServer(con, x)             # keeps 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
+
+# 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
+}
+}
+
+\keyword{ IO }
+\keyword{ utilities }


Property changes on: pkg/svSocket/man/evalServer.Rd
___________________________________________________________________
Name: svn:executable
   + *



More information about the Sciviews-commits mailing list