如何使用R连接两台计算机? [英] How to connect two computers using R?
问题描述
是否可以在两台不同计算机上的两个R会话之间打开通信流?
Is it possible to open a communication stream between two R sessions on two different computers?
如果两个会话都在同一台计算机上,我会使用套接字来连接会话。我想在两台不同的计算机上,我应该尝试使用网络套接字。 httpuv
支持R作为Web套接字服务器,但不幸的是,我找不到任何支持R中客户端Web套接字的最新软件包。
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.
推荐答案
服务器示例://github.com/corynissen/r-socket-server rel = nofollow>套接字在计算机之间进行通讯。
I have used sockets to communicate between computers using R.
服务器示例:
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()
客户端示例:
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
这篇关于如何使用R连接两台计算机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!