[Rd] pipe(): input to, and output from, a single process

Gábor Csárdi c@@rd|@g@bor @end|ng |rom gm@||@com
Mon Mar 16 13:12:58 CET 2020


I am not sure if `pipe()` works for this, but if it turns out that it
does not, then you can use the processx package, e.g.:

> p <- processx::process$new("sed", c("-l", "s/a/x/g"), stdin = "|", stdout = "|")
> p$write_input("foobar\n")
> p$read_output()
[1] "foobxr\n"

The `-l` sed flag is to make sed line-buffered, otherwise it is will
not produce output until there is enough.

`$write_input()` and `$read_output()` are not easy to program, in particular:
* `$write_input()` returns the chunk of data that it hasn't managed to
write into the pipe. You need to call `$write_input() again, with this
data next, usually.
* `$read_output()` returns an empty string if there is no data to
read, so typically you want to call `p$poll()` first, to make sure
that there is something to read.
* `$read_output()` might not read whole lines, so maybe
`$read_output_lines()` is better for you.
* Close the stdin of the process if you want to quit cleanly:
`close(p$get_input_connection())`.
* There is currently no way to poll the input side of the pipe. :(

HTH, Gabor

On Mon, Mar 16, 2020 at 11:31 AM Greg Minshall <minshall using umich.edu> wrote:
>
> hi.  i'd like to instantiate sed(1), send it some input, and retrieve
> its output, all via pipes (rather than an intermediate file).
>
> my sense from pipe and looking at the sources (sys-unix.c) is that is
> not possible.  is that true?  are there any thoughts of providing such a
> facility?
>
> cheers, Greg
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list