Obtaining pipe exit status in R

I use the R pipe() function to capture the output from a shell command, but I would also like to get the exit code from the command.

I know that I could use system2 here, but I need an advantage in the channel, that is, the ability to process the output in a streaming manner.

I am considering writing my own library to wrap the popen() and pclose() C functions in order to take advantage of the fact that pclose() returns the exit status, but perhaps this can be avoided.

Any suggestions? Thanks!

Note

Of course, there are ways to do this with temporary files, named pipes, etc., but I would ideally want to avoid these workarounds. I am ready to compile a common library in it with the function R-> C (and I am even ready to copy part of the source code R), but I do not want to rebuild R.

Update

I started reading the R source code and found an unverified pclose call:

in src/main/connections.c :

 static void pipe_close(Rconnection con) { pclose(((Rfileconn)(con->private))->fp); con->isopen = FALSE; } 

I tried to move on to the implementation of the R_pclose C function, which duplicates the R code for close() , but retains this return value. Unfortunately, I came across this static variable in src/main/connections.c

 static Rconnection Connections[NCONNECTIONS]; 

Since I would have to run objcopy --globalize-symbol=Connections libR.so /path/to/my/libR.so anyway to access the variable, it seems like my best solution is to rebuild R using my own patch to fix the return value of pclose.

+7
source share
1 answer

Awful hack: you can wrap your command call in a small shell script that writes the exit code of its child to some temporary file. Therefore, when the stream has ended, you can wait until this file is of zero size, and then read the status from there. Hope someone comes up with a better solution, but at least it's a kind of solution.

+1
source

All Articles