Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

r - Piping Rscript gives error after output

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.

  • Piping STDOUT to /dev/null returns only the error message
  • Piping STDERR to /dev/null returns only the error message...!

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 here, but it's not immediately clear to me how this would relate to an R script.

Edit 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)),'
') # 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

Edit 2 It's replicable with readLines so I've removed JSON library-specific details in the example above. Script and dummy JSON gisted here.

Edit 3 It seems it may be possible to take command-line arguments including pipes and pass them to pipe() - I'll try this when I can and resolve the question.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 might not 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.


The command line solution:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...