TL;DR
What are the (possibly unwanted) side-effects of using knit()
/knit2pdf()
instead of the "Compile PDF"1 button in RStudio?
Motivation
Most users of knitr
seem to write their documents in RStudio and compile the documents using the "Compile PDF" / "Knit HTML" button. This works smoothly most of the time, but every once a while there are special requirements that cannot be achieved using the compile button. In these cases, the solution is usually to call knit()
/knit2pdf()
/rmarkdown::render()
(or similar functions) directly.
Some examples:
- How to knit/Sweave to a different file name?
- Is there a way to knitr markdown straight out of your workspace using RStudio?
- Insert date in filename while knitting document using RStudio Knit button
Using knit2pdf()
instead of the "Compile PDF" button usually offers a simple solution to such questions. However, this comes at a price: There is the fundamental difference that "Compile PDF" processes the document in a separate process and environment whereas knit2pdf()
and friends don't.
This has implications and the problem is that not all of these implications are obvious. Take the fact that knit()
uses objects from the global environment (whereas "Compile PDF" does not) as an example. This might be obvious and the desired behavior in cases like the second example above, but it is an unexpected consequence when knit()
is used to overcome problems like in example 1 and 3.
Moreover, there are more subtle differences:
- The working directory might not be set as expected.
- Packages need to be loaded.
- Some options that are usually set by RStudio may have unexpected values.
The Question and it's goal
Whenever I read/write the advice to use knit2pdf()
instead of "Compile PDF", I think "correct, but the user should understand the consequences …".
Therefore, the question here is:
What are the (possibly unwanted) side-effects of using
knit()
/knit2pdf()
instead of the "Compile PDF" button in RStudio?
If there was a comprehensive (community wiki?) answer to this question, it could be linked in future answers that suggest using knit2pdf()
.
Related Questions
There are dozens of related questions to this one. However, they either propose only code to (more or less) reproduce the behavior of the RStudio button or they explain what "basically" happens without mentioning the possible pitfalls. Others look like being very similar questions but turn out to be a (very) special case of it. Some examples:
- Knit2html not replicating functionality of Knit HTML button in R Studio: Caching issue.
- HTML outputs are different between using knitr in Rstudio & knit2html in command line: Markdown versions.
- How to convert R Markdown to HTML? I.e., What does “Knit HTML” do in Rstudio 0.96?: Rather superficial answer by Yihui (explains what "basically" happens) and some options how to reproduce the behavior of the RStudio button. Neither the suggested
Sys.sleep(30)
nor the "Compile PDF" log are insightful (both hints point to the same thing). - What does “Knit HTML” do in Rstudio 0.98?: Reproduce behavior of button.
About the answer
I think this question raised many of the issues that should be part of an answer. However, there might be many more aspects I don't know about which is the reason why I am reluctant to self-answer this question (though I might try if nobody answers).
Probably, an answer should cover three main points:
- The new session vs. current session issue (global options, working directory, loaded packages, …).
- A consequence of the first point: The fact that
knit()
uses objects from the calling environment (default:envir = parent.frame()
) and implications for reproducibility. I tried to tackle the issue of preventingknit()
from using objects from outside the document in this answer (second bullet point). - Things RStudio secretly does …
- … when starting an interactive session (example) --> Not available when hitting "Compile PDF"
- … when hitting "Compile PDF" (anything special besides the new session with the working directory set to the file processed?)
I am not sure about the right perspective on the issue. I think both, "What happens when I hit 'Compile PDF' + implications" as well as "What happens when I use knit()
+ implications" is a good approach to tackle the question.
1 The same applies to the "Knit HTML" button when writing RMD documents.