
Question:
I wrote a small R script to read JSON, which works fine but upon piping with
Rscript myscript.R | head
the (full, expected) output comes back with an error
Error: ignoring SIGPIPE signal
Execution halted
Oddly I can't remove this by piping STDERR to /dev/null
using:
Rscript myscript.R | head 2>/dev/null
The same error is given... presumably because the error arises within the Rscript command? The suggestion to me is that the output of the head command is all STDOUT.
<ul><li>Piping STDOUT to/dev/null
returns only the error message</li>
<li>Piping STDERR to /dev/null
returns only the error message...!</li>
</ul>Piping the output to cat seems to be 'invisible' - this doesn't cause an error.
Rscript myscript.R | cat | head
Further pipe chaining is possible after the cat command but it feels like I may be ignoring something important by not addressing the error.
Is there a setting I need to use within the script to permit piping without the error? I'd like to have R scripts at the ready for small tasks as is done with the likes of Python and Perl, and it'd get annoying to always have to add a useless cat
.
There is discussion of handling this error in C <a href="https://stackoverflow.com/questions/7774569/using-signals-and-sigpipe" rel="nofollow">here</a>, but it's not immediately clear to me how this would relate to an R script.
<strong>Edit</strong> In response to @lll's answer, the full script in use (above called as 'myscript.R') is
library(RJSONIO)
note.list <- c('abcdefg.json','hijklmn.json')
# unique IDs for markdown notes stored in JSON by Laverna, http://laverna.cc
for (laverna.note in note.list) {
# note.file <- path.expand(file.path('~/Dropbox/Apps/Laverna/notes',
# laverna.note))
# For the purpose of this example run the script in the same
# directory as the JSON files
note.file <- path.expand(file.path(getwd(),laverna.note))
file.conn <- file(note.file)
suppressWarnings( # warnings re: no terminating newline
cat(paste0(substr(readLines(file.conn), 2, 15)),'\n') # add said newline
)
close(file.conn)
}
Rscript myscript.R
outputs
"id":"abcdefg"
"id":"hijklmn"
Rscript myscript.R | head -1
outputs
"id":"abcdefg"
Error: ignoring SIGPIPE signal
Execution halted
It's not clear to me what would be terminating 'early' here
<strong>Edit 2</strong> It's replicable with readLines
so I've removed JSON library-specific details in the example above. Script and dummy JSON gisted <a href="https://gist.github.com/lmmx/9bdec10b4307d640fade" rel="nofollow">here</a>.
<strong>Edit 3</strong> It seems it may be possible to take command-line arguments <em>including</em> pipes and pass them to pipe()
- I'll try this when I can and resolve the question.
The error is simply caused by an attempt to write to the pipe without a process connected to the other end. In other words, your script has already picked up and left by the time the pipe is reached and the HEAD
command is called.
The command itself <em>might not</em> be the issue; it could be something within the script causing an early termination or race condition before reaching the pipe. Since you're getting full output it may not be that much of a concern, however, masking the error with other CLI
commands as mentioned probably isn't the best approach.
<strong>The command line solution:</strong>
R
does have a couple of useful commands for dealing with instances in which you might want the interpreter to wait, or perhaps suppress any errors that would normally be output to stderr.
For command-line R, error messages written to ‘stderr’ will be sent to
the terminal unless ignore.stderr = TRUE
. They can be captured (in the
most likely shells) by:
system("some command 2>&1", intern = TRUE)
There is also the wait
argument which could help with keeping the process alive.
wait
— logical (not NA) indicating whether the R interpreter should
wait for the command to finish, or run it asynchronously. This will be
ignored (and the interpreter will always wait)
if intern = TRUE
.
system("Rscript myscript.R | head 2>&1", intern = TRUE)
The above would wait, and output errors, if any are thrown.
system("Rscript myscript.R | head", intern = FALSE, ignore.stderr = TRUE)
The above won't wait, but would suppress errors, if any.