Knit PDF produced PDF different than Source in RStudio - r

When I knit PDF on RStudio. It DOES NOT match what is in my Rmd file after source of the .Rmd. This has happened every time I tried to Knit PDF. How can I get example what is in my .Rmd into Knit PDF? I've tried clearing cache and restarting RStudio but this inconsistency is still happening...

You need to set.seed() at instance where you are generating random data if you are generating any.
Try to clean your environment and rerun and assess results
Try to set.seed at the top of the code and rerun.

Related

Knitting a Markdown pdf with only a few modifications

I have an R Markdown script that takes more than two hours to run.
Sometimes I just want to make a small modification and knit it again to a pdf, but it seems like a waste of time to run the whole script for hours just for that little change.
Is there any way I can "save" what I've already knitted, so that when I knit again with a small change, it can knit only the part where it's changed?

R Markdown not knitting when I add a new plot

My R markdown file is working fine, until I add a particular plot, then it will not knit and displays the following error:
! LaTeX Error: File `filename_files/figure-latex/unnamed-chunk-2-1' not found.
When I remove the plot, the file knits, when i include it, it doesn't knit. Here is the code I'm using for the plot:
```{r echo=FALSE}
plot(x=variable1,y=variable2,main="plot title")
```
I'm at a loss as to why this isn't working, when I've got other plots in the document that are displaying fine and are knitting in the pdf.
Rather frustratingly, I have managed to get around the issue by copying all of the text into a new document, and then trying to knit, and it worked straight away... However, after a while, the same issue kept occurring, this time with a different plot. A further workaround seemed to be rebooting the laptop. Again, I'm not entirely sure what is causing the issue, but it was occurring intermittently and without warning.

knitr Minimal Demo not compiling correctly

Hi knitr experts: I cannot for the life of me figure out how to compile the knitr Miminal Demo .Rnw document correctly. When I download and run Yihui's Minimal Demo of knitr .Rnw file (link), the document compiles but incorrectly handles the R chunks:
I changed nothing, just opened and compiled. Help welcome. As an aside, would very much appreciate tips on whether this is how I should be going about adding R code into a latex template that UT-Austin requires for dissertation publication. Thanks.
I can reproduce your PDF file when using Sweave instead of knitr for translating the .Rnw file. When switching to knitr via the RStudio options (c.f. https://support.rstudio.com/hc/en-us/articles/200532247), I get an error message and no PDF, which probably reproduces #r2evans' results in the comments. I also get a warning message that line 19 is Sweave specific (\SweaveOpts{concordance=TRUE}). Removing that line, the file processes with knitr correctly producing

Why the chunk could be run but there is an error when I knit in r-markdown?

Why can the chunk be run interactively but there is an error when I try to knit the file? When I run each chunk, there is no error, but when I knit it, it is stopped by an error.
The usual reason for a difference between the interactive result and the knitted document result is that you are referring to objects that aren't defined in the document. Interactive code can see your global environment, knitted code can't.

What are the commands executed if I press the "Compile PDF" button on a "Rnw" file in RStudio?

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.
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.

Resources