如何捕获CLI工具文件输出到R对象或stdout? [英] How can I capture CLI tool file output to R object or stdout?

查看:110
本文介绍了如何捕获CLI工具文件输出到R对象或stdout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用许多命令行界面(CLI)工具(例如 texi2pdf pdf2svg ,并且我想直接将这些工具的输出 file 捕获为R对象,不接触文件系统.

I'm calling a bunch of command-line interface (CLI) tools (such as texi2pdf or pdf2svg from an R script, and I'd like to capture the output file of these tools directly as an R object, without touching the file system.

这与更常见的我如何将stdout重定向到文件"问题相对应. (也许暗示我在错误地使用它").

This is the opposite concern of the more frequent "how-do-I-redirect-stdout-to-file"-question. (Perhaps that implies that I'm "using it wrong").

示例:

说,我有一个要编译的简单的乳胶reprex.tex文件:

Say, I have a simple latex reprex.tex file that I'd like to compile:

\documentclass{article}

\begin{document}

Foo.


\end{document}

在R中,我可以使用tools::texi2pdf()包装器,也可以自己发送命令以将其编译为pdf.

In R, I can use the tools::texi2pdf() wrapper, or I can send the command myself to compile this to a pdf.

在外壳上,只需:

texi2pdf reprex.tex

等效地,从R中调用:

reprex_as_pdf <- system2(command = "texi2pdf", args = c(reprex.tex), stdout = TRUE, stderr = TRUE)

system2()方便地使我可以通过stdout = TRUE将stdout/stderr捕获为字符向量,这使我成功了一半.

Conveniently, system2() allows me to capture stdout/stderr as a character vector via stdout = TRUE, which gets me half of the way.

但是,我似乎无法在 texi2pdf手册页中找不到任何内容这样,我就可以将(二进制!)输出重定向到stdout(然后,重定向到system2()).

However, I can't seem to find anything in the texi2pdf manpage that would allow me to redirect the (binary!) output to stdout (and that way, to system2()).

如何将R中texi2pdf 的输出直接捕获为(原始)向量?

How can I capture the output of texi2pdf directly in R as a (raw) vector?

(奖金:我还如何将 input 作为R字符向量而不是文件传递给texi2pdf?)

(Bonus: how can I also pass the input to texi2pdf as an R character vector, rather than as a file?)

全职工作

我当然可以通过tempfile()进行工作,但是接触文件系统,并且看起来不雅/麻烦.

I can of course work via a tempfile(), but that would touch the filesystem, and just seems inelegant / cumbersome.

library(readr)
system2(command = "texi2pdf",
        args = c("reprex.tex"),
        stdout = TRUE,
        stderr = TRUE)
reprex_as_pdf <- read_file_raw("reprex.pdf")


为什么,您会问有人要这样做吗?


Why would anyone want to do this, you ask?

我通常害怕副作用和跨计算机/OS文件系统的恶作剧,并且想要将副作用隔离到很少的功能. 而且,实际上会以编程方式导出pdf,并以各种不同的功能对其进行转换. 最后,我需要这些pdf的很多,并且我希望它们在部署到可能没有texi2dvi 的服务器之前能够轻松地编译和缓存.

I'm generally scared of side-effects and cross-machine/OS file system shenanigans, and want to isolate the side effects to very few functions. Also, the pdf will actually be programmatically exported, converted in all sorts of different functions. Lastly, I need a lot of these pdfs, and I want them easily compiled and cached before deploying to a server which may not have texi2dvi.

如果我绝对是错误地使用",请阻止我.

Please stop me if I am just absolutely "using it wrong".

推荐答案

简单地输入:通常情况下,您不能.但是有时这些工具允许您指定输出文件,在这种情况下,您可以(在某些系统上,但请注意,这是不可移植的)将/dev/stdout指定为输出文件.

Put simply: in the general case you can’t. But sometimes these tools allow you to specify an output file, in which case you can (on some systems, but note that this is not portable) specify /dev/stdout as the output file.

根据texi2pdf联机帮助页,因此应该可以进行以下操作:

According to the texi2pdf manpage, the following should therefore work:

reprex_as_pdf <- system2(
    command = "texi2pdf",
    args = c("reprex.tex", "-o", "/dev/stdout"),
    stdout = TRUE, stderr = TRUE
)

但是,这不会阻止该工具以其他方式(创建临时文件等)接触文件系统.无法防止这种情况,也不希望如此:这些效果对用户应该是透明的.唯一的例外是生成了多个输出文件(例如日志文件),不幸的是,TeX相关工具无法正常工作.

However, this doesn’t prevent the tool from touching the file system in other ways (creating temporary files etc). There’s no way to prevent this, nor would it be desirable: these effects should be transparent to the user. The exception is the generation of multiple output files (e.g. log files), which TeX related tools unfortunately do prolifically.

要回答您的奖励问题:这再次不可能以平台无关的方式实现,但是在POSIX系统上,您可以创建

To answer your bonus question: this is once again not possible in a platform independent way but on POSIX systems you can create a named pipe. However, for all intents and purposes from an R perspective this behaves like an ordinary file, and it’s managed by the file system.

这篇关于如何捕获CLI工具文件输出到R对象或stdout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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