5069

Piping Rscript gives error after output

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.

Answer1:

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.

<hr />

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

<blockquote>

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:

</blockquote> system("some command 2>&1", intern = TRUE)

There is also the wait argument which could help with keeping the process alive.

<blockquote>

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.

</blockquote> 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.

Recommend

  • java simulate user input to external program
  • How to prevent choosePrivateKeyAlias dialog in android app?
  • IE7 issues with my jQuery [click and change functions]
  • Customize Django admin template
  • Issue a POST request with url_for in Flask
  • Neo4j Cypher WITH is required between CREATE and MATCH
  • web scraping in R
  • Adjust View for keyboard appears when switching UITextField (Swift)
  • jQuery 'click' event doesn't work on iOS Safari?
  • How to stop the python turtle from drawing
  • ActivityThread.java Null pointer exception
  • Spotfire Custom Expression : Calculate (Num/Den) Percentages
  • Initializing a pointer to compound literals in C
  • Modification of innerHTML stored in variable not working
  • C# Create Control From a String Value
  • Background beacon detection and Notification (Both iOS and Android) for Eddystone beacon?
  • Azure NodeJS Error: ENOENT, open 'D:\\home\\site\\wwwroot\\bin\\views\\'
  • what do lines starting with double-slash '//' mean in a .npmrc?
  • How to move to lines with the same indentation in Visual Studio Code
  • Unable to run testNG tests from maven
  • separate tokens in batch file
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • Copy and paste data from multiple workbooks to a worksheet in another Workbook
  • Ajax call on Multiple selection in Select box
  • How to create subsets of a single set of elements with XSLT?
  • java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/SpringDispatcher-ser
  • Typeahead.js does give me suggestions but doesn't select them
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • How to configure the APNS.Certificate in the arm template
  • Geokit in Ruby on Rails, problem with acts_as_mappable
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • how to get the location(lat/lng) on google maps v3 from the location(x,y)
  • Android: Unable to detect vertical plane
  • Send array to next viewcontroller iOs xcode [duplicate]
  • Excel VBA : conditional formatting of sheet1 cells from sheet2 values in excel 2007
  • Codeigniniter insert data through models and controller