How can I capture the output of a CLI tool file into an R or stdout object?

I am calling a command line interface (CLI) toolbox (for example, texi2pdf

or pdf2svg

from an R script, and I would like to capture the output of these tools directly as an R object , without touching the file system .

This is the opposite concern for the more frequent "how-do-I-redirect-stdout-to-file" -question. (This may mean that I am "using this incorrectly").

Example :

Let's say I have a simple latex file reprex.tex

that I would like to compile:

\documentclass{article}

\begin{document}

Foo.


\end{document}

      

In R, I can use a wrapper tools::texi2pdf()

, or I can send the command myself to compile it to pdf.

On the shell, it's simple:

texi2pdf reprex.tex

      

Equivalently called from R:

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

      

Conveniently, system2()

allows me to write stdout / stderr as a character vector through stdout = TRUE

, which gives me half the way.

However, I cannot find anything in the texi2pdf manpage that would allow me to redirect (binary!) Output to stdout

(and thus to system2()

).

How can I write the output texi2pdf

directly to R as a (source) vector?

(Bonus: how can I pass the input to texi2pdf

as an R character vector and not as a file?)


roundabout

I can, of course, work through tempfile()

, but that would touch on the filesystem and would just seem 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?

I am generally afraid of the side effects and shenanigans of the OS file system / file system and want to highlight the side effects for very few functions. In addition, the pdf will actually be exported programmatically, converted to all sorts of functions. Finally, I need a lot of these PDFs, and I want them to be easily compiled and cached before deploying to a server that I may not have texi2dvi

.

Please stop me if I'm just "wrong".

+1


source to share


1 answer


To put it simply: in general, you cannot . 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.

According to the texi2pdf

man page, the following should work:

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

      



However, this does not prevent the application from accessing the file system in other ways (creating temporary files, etc.). Theres no way to prevent this, and would not be desirable: these effects must be transparent to the user. An exception is the generation of several output files (eg log files), which unfortunately include TeX-related tools.

To answer your bonus question: this again is not possible for platform independent, but on POSIX systems you can create a named pipe.However , for all intents and purposes from R's point of view, this behaves like a regular file and is managed by the filesystem.

+1


source







All Articles