Once again I am in the situation that I want to replicate what is happening when I press the Compile PDF button on an .Rnw file in RStudio with my own R script.
For example I create a new .Rnw
file in RStudio with File > New File > R Sweave. It looks like this:
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<echo=F, results=tex>>=
library(xtable)
xtable(mtcars)
@
\end{document}
I inserted a chunk with alt+cmd+i and used the autocompletion to set the chunk options. Hence I assume I did everything with the default settings as RStudio assumes it to be. When I press Compile PDF everything works without problems. But when I execute:
knitr::knit2pdf("Sweave-test.Rnw")
I get an Error "It seems you are using the Sweave-specific syntax". Hence there are additional steps needed.
What I came up with so far is the following:
library(knitr)
tempfile1 <- tempfile(fileext=".Rnw")
Sweave2knitr(file = "input.Rnw", output = tempfile1)
tempfile2 <- tempfile(fileext=".tex")
knit(tempfile1, output=tempfile2)
tools::texi2pdf(tempfile2, clean=T)
system(paste("open", sub(".tex", ".pdf", basename(tempfile2))))
(The last line is OSX specific I think).
But I am curious to know what RStudio is doing exactly. I looked into the RStudio github page but am not sure where to find the command. Other Stackoverlow questions show that there are slight differences between what the button does and knit2pdf
. It seems the question has been asked over and over again, also in relation to the Knit Html button. It might use functions from the knitr
package, the markdown
, the rmarkdown
package, texi2pdf
from the tools
package, Sweave
from the utils
package or pandoc
. I have no Idea...
Related question (that all got some rather vague answers):
- Difference: "Compile PDF" button in RStudio vs. knit() and knit2pdf()
- Difference between "Compile PDF" and knit2pdf
- How to convert R Markdown to HTML? I.e., What does "Knit HTML" do in Rstudio 0.96?
- What does “Knit HTML” do in Rstudio 0.98?
- Compile .Rnw file with command
- How to build Knitr document from the command line
Currently I am using RStudio 1.0.136.