在R中将其关闭之前,如何从管道连接获取输出? [英] how to get output from a pipe connection before closing it in R?

查看:116
本文介绍了在R中将其关闭之前,如何从管道连接获取输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,我们可以使用pipe()打开管道连接并对其进行写入.我观察到以下情况,我不太了解.我们以python管道为例:

In R, we can open a pipe connection using pipe() and write to it. I observed the following situation that I do not quite understand. Let's use a python pipe for example:

z = pipe('python', open='w+')

cat('x=1\n', file=z)
cat('print(x)\n', file=z)
cat('print(x+2)\n', file=z)
cat('print(x+2\n', file=z)
cat(')\n', file=z)

close(z)

我期望的是print()的输出将立即显示在R控制台中,但事实是输出仅在我关闭管道连接之后出现:

What I was expecting was the output from print() would be immediately shown in the R console, but the fact is the output comes only after I close the pipe connection:

> z = pipe('python', open='w+')
> 
> cat('x=1\n', file=z)
> cat('print(x)\n', file=z)
> cat('print(x+2)\n', file=z)
> cat('print(x+2\n', file=z)
> cat(')\n', file=z)
> 
> close(z)
1
3
3

所以我的问题是,在关闭连接之前如何获得输出?请注意,似乎无法使用capture.output()捕获输出:

So my question is, how can I get the output before I close the connection? Note that it does not seem to be possible to capture the output using capture.output(), either:

> z = pipe('python', open='w+')
> 
> cat('x=1\n', file=z)
> cat('print(x)\n', file=z)
> cat('print(x+2)\n', file=z)
> cat('print(x+2\n', file=z)
> cat(')\n', file=z)
> 
> x = capture.output(close(z))
1
3
3
> x
character(0)

该问题的背景是 knitr引擎.对于像Python这样的解释型语言,我希望可以打开一个持久的终端",这样我就可以继续向其中编写代码并从中获取输出.不过,我不确定pipe()是否是正确的方法.

The background of this question is the knitr engines. For the interpreted languages like Python, I wish I can open a persistent "terminal" so that I can keep on writing code into it and get output from it. I'm not sure if pipe() is the correct way to go, though.

推荐答案

Python注意到输入不是交互式的,并等待直到关闭连接才能解析并执行代码.您可以使用-i选项强制其保持交互模式. (但是输出有些混乱).

Python notices that the input is not interactive and waits until the connection is closed to parse and execute the code. You can use the -i option to force it to stay in interactive mode. (but the output is a bit mangled).

z = pipe('python -i', open='w')
cat('x=1\n', file=z)
cat('print(x)\n', file=z)
cat('print(x+2)\n', file=z)
cat('print(x+2\n', file=z)
cat(')\n', file=z)
Sys.sleep(2)
# Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
# [GCC 4.7.3] on linux2
# Type "help", "copyright", "credits" or "license" for more information.
# >>> >>> 1
# >>> 3
# >>> ... 3
# >>> 
close(z)

您的实际问题更加复杂:您需要同时读取和写入同一连接. 我不知道如何以可移植的方式进行操作,但是您可以在支持管道和命名管道的平台上使用管道和命名管道.

Your actual problem is more complicated: you need to both read and write to the same connection. I do not know how to do that in a portable way, but you can use a pipe and a named pipe (a "fifo") on platforms that support them.

stopifnot( capabilities("fifo") )
system('mkfifo /tmp/Rpython.fifo')
output <- fifo('/tmp/Rpython.fifo', 'r')
input  <- pipe('python -i > /tmp/Rpython.fifo', 'w')
python_code <- "
x=1
print(x)
print(x+2)
print(x+2
)
"
cat( python_code, file = input )
flush( input )
Sys.sleep(2) # Wait for the results
result <- readLines(output)
result
# [1] "1" "3" "3"

这篇关于在R中将其关闭之前,如何从管道连接获取输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆