Hi,
I am not clear what commands R-Studio runs when the "Compile PDF" button is clicked.
I am using Sweave and a Rnw file to produce a PDF. Using R studio, when I click the "Compile PDF" button the Rnw file is processed and the PDF is displayed (My local version of R-Studio is configured to use knitr, "pdflatex" to compile the files and the system viewer to display the compiled PDF).
However, I cannot do the same things in an R script.
I have run this command:
Sweave("template.Rnw")
and gotten a message that a tex file has been written. I don't know how to compile this tex file within R.
When I try
knit2pdf("template.Rnw")
I get the following error:
output file: template.tex
Error: Failed to compile template.tex.
In addition: Warning message:
running command '"pdflatex" -halt-on-error -interaction=batchmode "template.tex"' had status 1
So, how do I replicate the commands run by the "Compile PDF" button in an R script?
I figured the commands out:
knit("template.Rnw") # created the tex file
tools::texi2pdf("template.tex") # creates the pdf
system('open "template.pdf"') #displays the pdf, once displayed the adobe pdf viewer give users the option to save.
I'll integrate this into my shiny app and things should be okay.
Right now, I'm running windows 7 with miktex 2.9 profiding the pdfLatex instance. In production, this will move to a docker container on out server.
I'd like to generalize this into a function that does the same, but gives a few more options: quiet for knitr & clean for texi2pdf. The function below does this, but I can't replicate the effect of the system command to open the PDF on Windows.
# Compile an Rnw to PDF using the same commands as R Studio
# but offer the options to control the quiet & clean settings
# from: https://forum.posit.co/t/what-commands-are-run-when-the-compile-pdf-button-is-clicked/6291
compile_pdf <- function(input, output=NULL, quiet=FALSE, clean=FALSE, ...) {
knitr::knit(input, output, ...)
tools::texi2pdf(output, clean=clean )
system(paste('open', output)) # what is the equivalent for Windows?
}