pipe - How can I capture CLI tool file output to R object or stdout? -


i'm calling bunch of command-line interface (cli) tools (such texi2pdf or pdf2svg r script, , i'd capture output file of these tools directly r object, without touching file system.

this opposite concern of more frequent "how-do-i-redirect-stdout-to-file"-question. (perhaps implies i'm "using wrong").

example:

say, have simple latex reprex.tex file i'd compile:

\documentclass{article}  \begin{document}  foo.   \end{document} 

in r, can use tools::texi2pdf() wrapper, or can send command myself compile pdf.

on shell, simply:

texi2pdf reprex.tex 

equivalently, called r:

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

conveniently, system2() allows me capture stdout/stderr character vector via stdout = true, gets me half of way.

however, can't seem find in texi2pdf manpage allow me redirect (binary!) output stdout (and way, system2()).

how can capture output of texi2pdf directly in r (raw) vector?

(bonus: how can pass input texi2pdf r character vector, rather file?)


work-around

i can of course work via tempfile(), would touch filesystem, , 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 want this, ask?

i'm scared of side-effects , cross-machine/os file system shenanigans, , want isolate side effects few functions. also, pdf programmatically exported, converted in sorts of different functions. lastly, need lot of these pdfs, , want them compiled , cached before deploying server may not have texi2dvi.

please stop me if absolutely "using wrong".

put simply: in general case you can’t. these tools allow specify output file, in case can (on systems, note not portable) specify /dev/stdout output file.

according texi2pdf manpage, following should therefore work:

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

however, doesn’t prevent tool touching file system in other ways (creating temporary files etc). there’s no way prevent this, nor desirable: these effects should transparent user. exception generation of multiple output files (e.g. log files), tex related tools unfortunately prolifically.

to answer bonus question: once again not possible in platform independent way on posix systems can create named pipe. however, intents , purposes r perspective behaves ordinary file, , it’s managed file system.


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -