How to connect two computers using R?

末鹿安然 提交于 2019-12-12 12:14:11

问题


Is it possible to open a communication stream between two R sessions on two different computers?

I use sockets to connect sessions if both are on the same computer. I suppose with two different computers, I should try web sockets. httpuv supports R as a web socket server, but unfortunately, I could not find any up-to-date package that supports a client web socket in R.

I'm not tied to using web sockets. Any solution that enables communicating between computers in a real time manner would work.


回答1:


I have used sockets to communicate between computers using R.

server example:

import socket

server <- function(){
  while(TRUE){
    writeLines("Listening...")
    con <- socketConnection(host="localhost", port = 6011, blocking=TRUE,
                            server=TRUE, open="r+")
    data <- readLines(con, 1)
    print(data)
    response <- toupper(data) 
    writeLines(response, con) 
    close(con)
  }
}
server()

client example:

import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 6011))
while 1:
    data = raw_input ( "Enter text to be upper-cased, q to quit\n" )
    client_socket.send(data)
    if ( data == 'q' or data == 'Q'):
        client_socket.close()
        break;
    else:        
        data = client_socket.recv(5000)
        print "Your upper cased text:  " , data


来源:https://stackoverflow.com/questions/35849356/how-to-connect-two-computers-using-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!