[R] R as a programming language

Gregory Warnes gregory.warnes at mac.com
Fri Nov 9 00:43:57 CET 2007


>
>     (2) More process and I/O facilities, specifically I'd like  
> forking and
>       something like a "functionconnection" which works like a
>       textconnection but obtains input from / feeds output to a  
> function.
>       This would allow running an external process that receives input
>       *and* provides output to R -- currently, pipes allow either one
>       or the other, but not both.

Both of these features are provided by the 'fork' package, at least  
on *nix systems.  From the example section of fork::fork,

      # Use a socket to communicate between two processes.  Information
      # typed on the console, which is read by the initial process,  
will be send
      # to the child process for display.
      ###
      ## Not run:
      send <- function()
        {
           pid <- getpid()
           con1 <- socketConnection(Sys.info()["nodename"], port = 6011)
           i <- 1
           while(TRUE)
             {
               cat("[",pid,"] ",i,": ",sep="")
               data <- readLines(stdin(), n=1)
               writeLines(data, con=con1)
               if( length(grep("quit", data))>0 )
                  break;
               i <- i+1
             }
           close(con1)
        }

      recieve <- function()
        {
           pid <- getpid()
           con2 <- socketConnection(port = 6011, block=TRUE,  
server=TRUE)
           i <- 1
           while(TRUE)
             {
                data <- readLines(con2, n=1)
                cat("[",pid,"] ",i,": ",sep="")
                writeLines(data, stdout())
                if( length(grep("quit", data))>0 )
                    break;
                i <- i+1
             }
           close(con2)
        }

      ## Not run:
      # Important: if we aren't going to explicitly wait() for the child
      # process to exit, we need to tell the OS that we are ignoring  
child
      # process signals so it will let us die cleanly.
      signal("SIGCHLD","ignore")
      ## End(Not run)

      # fork off the process
      pid <- fork(recieve)


      # start sending...
      send()
      ## End(Not run)



-Greg


> With these two, I'd be quite inclined to use R as my primary scripting
> language. (I tried to get there a while ago and wrote a "filterpipe"
> patch to achieve (2), currently I work use a mix of Python and R).
>
> Best regards, Jan
> -- 
>  +- Jan T. Kim  
> -------------------------------------------------------+
>  |             email:  
> jtk at cmp.uea.ac.uk                               |
>  |             WWW:   http://www.cmp.uea.ac.uk/people/ 
> jtk             |
>  *-----=<  hierarchical systems are for files, not for humans   
> >=-----*
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting- 
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list