Szabolcs
Szabolcs

Reputation: 25703

Is it possible to push/pull variables between two instances of R?

Suppose I have two instances of R running. Are there existing solutions to easily send variables/data from one instance to the other? Maybe even synchronize the values of a variable between the two instances?

For example, first the two instances (R1 & R2) would be connected somehow, then in R1:

> a <- 12
> push(a)

and at this point in R2:

> a
[1] 12

The keyword here is ease of use: make it as quick as possible (for the user) to interactively synchronize the value of certain variables. I would use this with Mathematica's RLink to work interactively in one R instance and push/pull data to/from Mathematica's instance.


I realize that the question might sound strange. The reason why I'm hopeful that something like this exists is that it would be useful for parallel or distributed computing as well (which is not my use case here).

Upvotes: 15

Views: 1990

Answers (3)

flodel
flodel

Reputation: 89057

Have a look at svSocket. From the package description at: svSocket.pdf

The SciViews svSocket package provides a stateful, multi-client and preemtive socket server.  [...] 

Although initially designed to server GUI clients, the R socket server can also be used to exchange data between separate R processes.

This demo video is really worth it.

Upvotes: 19

dickoa
dickoa

Reputation: 18437

I think Redis can help you achieve what you want. You can use the R packages rredis and/or RcppRedis

On the first instance of R you can do

library(rredis)
redisConnect()
redisSet("a", 12)
[1] "OK"

Then on the second R instance you can then do

library(rredis)
redisConnect()
redisGet("a")
[1] 12

Upvotes: 7

Scott Ritchie
Scott Ritchie

Reputation: 10543

This is a different approach to the push/pull model, but you can use the bigmemory package to create a matrix that exists in shared memory (or on disk) that can be accessed across multiple R sessions on the same machine:

R session 1

library(bigmemory)
m <- matrix(1:9, 3, 3)
m <- as.big.matrix(m, type="double", backingfile="m.bin", descriptorfile="m.desc")
m
# An object of class "big.matrix"
# Slot "address":
# <pointer: 0x7fba95004ee0>

R session 2

library(bigmemory)
m <- attach.big.matrix("m.desc") 
# Now any changes you make to m will be reflected in both sessions!

This is also useful for parallel computing using on matrices, since you're now only passing around the pointer to the matrix to each of the spawned R sessions, rather than the whole object.

Since we've created a file-backed big matrix, it also allows you to create matrices it also allows you to create and operate on matrices larger than memory!

Parallel example

library(bigmemory)
library(doMC) # Windows users will need to choose a different parallel backend
library(foreach)
registerDoMC(4) # number of cores (new R sessions to spawn) to run in parallel.

m <- matrix(rnorm(1000*1000), 1000)
as.big.matrix(m, type="double", backingfile="m.bin", descriptorfile="m.desc")
# Just to make sure we don't have any of these objects in memory when we spawn the 
# parallel sessions
rm(m)
gc()

foreach(i = 1:4) %dopar% {
  m <- attach.big.matrix("m.desc")
  # do something!
}

Upvotes: 10

Related Questions