输出后管道Rscript给出错误 [英] Piping Rscript gives error after output

查看:181
本文介绍了输出后管道Rscript给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小的R脚本来读取JSON,该脚本可以正常工作,但在与管道连接时

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

奇怪的是,我无法通过以下方式将STDERR管道输送到/dev/null:

Oddly I can't remove this by piping STDERR to /dev/null using:

Rscript myscript.R | head 2>/dev/null

给出相同的错误……大概是因为该错误出现在Rscript命令中?对我的建议是,head命令的输出全部为STDOUT.

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.

  • 将STDOUT连接到/dev/null仅返回错误消息
  • 将STDERR连接到/dev/null仅返回错误消息...!
  • Piping STDOUT to /dev/null returns only the error message
  • Piping STDERR to /dev/null returns only the error message...!

将输出管道输送到cat似乎是不可见的"-这不会导致错误.

Piping the output to cat seems to be 'invisible' - this doesn't cause an error.

Rscript myscript.R | cat | head

cat命令之后可以进行进一步的管道链接,但是感觉到我可能由于不解决错误而忽略了一些重要的事情.

Further pipe chaining is possible after the cat command but it feels like I may be ignoring something important by not addressing the error.

我是否需要在脚本中使用一个设置,以允许在没有错误的情况下进行管道传输?我想像一些Python和Perl一样,准备好R脚本来完成一些小任务,而总是不得不添加一个无用的cat会很烦人.

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.

此处中讨论了处理此错误的方法,但目前尚不清楚我将其与R脚本联系起来.

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.

编辑为响应@lll的回答,正在使用的完整脚本(以上称为"myscript.R")为

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)),'\n') # add said newline
  )
  close(file.conn)
}

Rscript myscript.R输出

"id":"abcdefg"
"id":"hijklmn" 

Rscript myscript.R | head -1输出

"id":"abcdefg" 
Error: ignoring SIGPIPE signal
Execution halted

我不清楚在这里终止早起"的原因

It's not clear to me what would be terminating 'early' here

编辑2 它可与readLines复制,因此在上面的示例中,我删除了特定于JSON库的详细信息.在此处.

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.

编辑3 似乎有可能采用命令行参数包括管道并将其传递给pipe()-我会在解决问题.

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.

推荐答案

该错误仅是由于尝试在不将进程连接到另一端的情况下写入管道而引起的.换句话说,在到达管道并调用HEAD命令时,脚本已经拾起并离开.

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.

命令本身可能不是问题;可能是脚本内的某些内容导致了到达管道之前的提前终止或争用情况.由于您将获得完整的输出,因此可能不必担心,因此,用上述的其他CLI命令掩盖错误可能不是最佳方法.

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.

命令行解决方案:

R确实具有一些有用的命令,用于处理您可能希望解释器等待或抑制通常会输出到stderr的任何错误的实例.

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.

对于命令行R,写入"stderr"的错误消息将发送至 终端,除非ignore.stderr = TRUE.它们可以被捕获(在 最可能的炮弹):

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)

还有wait参数可以帮助使进程保持活动状态.

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

wait —逻辑(不是NA),指示R解释器是否应 等待命令完成,或异步运行它.这将会 被忽略(解释器将始终等待) 如果是intern = TRUE.

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.

这篇关于输出后管道Rscript给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆