2

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):

Currently I am using RStudio 1.0.136.

Community
  • 1
  • 1
nnn
  • 4,985
  • 4
  • 24
  • 34

1 Answers1

0

I suspect it is just Sweave to turn a Rnw file into tex file and then calling pdflatex.exe to turn it from a tex file into a pdf file.

Sweave("Sweave-test.Rnw")
system("pdflatex.exe Sweave-test.tex")

And if you encountered a File Sweave.sty not found, you will need to add a path this file in your MiKTeX Options, please see here.

Community
  • 1
  • 1
chinsoon12
  • 25,005
  • 4
  • 25
  • 35