Search code examples
rsweavepdflatex

How to debug a Rsweave


I have an rsweave file that I run almost twice a week. Last time I used it a change a couple of things and when I run it to compile to pdf I got the following errors: enter image description here The pdf compiles complitly, and the only thing I notice that the error did is that the the pdf output has a extra page (the first one) all blank. I don't know how to make a reproducible example of the errors because I don't know whats the cause of it. But any way I just want to know generally how to debug a rsweave file when getting latex error like the ones in the picture


Solution

  • You don't say how you are running Sweave, but that looks like RStudio. To debug something like this, just run Sweave explicitly in the R console, e.g. if your input file is source.Rnw, run

    Sweave('source.Rnw')
    

    This will produce source.tex. Open that file in a text editor and look at the start of it. You will see that \Schunk is used on line 27, but \begin{document} doesn't occur until sometime later.

    My guess is that you added some text or a code chunk to the header. All text belongs after \begin{document}.

    Edited to add: It turns out from the comments below that you were using print(...) in a code chunk before \begin{document}. In Sweave, print output goes into the document. If you want a message to show up in the console log but not the document, use message("some text"). You'll also need to suppress the echoing of the command if you want to do this in the document header. For example,

    <<echo=FALSE,results=hide>>=
    message("Started at ", Sys.time())
    @
    

    will result in something like this in your console log:

     1 : keep.source term hide (Untitled.Rnw:6)
    Started at 2017-09-27 08:28:33
    

    and nothing in the document.