Difference: Compile PDF button in RStudio vs. knit() and knit2pdf()
First of all, I think this question is easier to answer if you limit the scope to the "Compile PDF" button, because the "Knit HTML" button is a different story. "Compile PDF" is only for Rnw documents (R + LaTeX, or think Sweave).
I'll answer your question following the three points you suggested:
Currently RStudio always launch a new R session to compile Rnw documents, and first changes the working directory to the directory of the Rnw file. You can imagine the process as a shell script like this:
cd path/to/your-Rnw-directory
Rscript -e "library(knitr); knit('your.Rnw')"
pdflatex your.texNote that the knitr package is always attached, and
pdflatex
might be other LaTeX engines (depending on your RStudio configurations for Sweave documents, e.g.,xelatex
). If you want to replicate it in your current R session, you may rewrite the script in R:owd = setwd("path/to/your-Rnw-directory")
system2("Rscript", c("-e", shQuote("library(knitr); knit('your.Rnw')"))
system2("pdflatex", "your.tex")
setwd(owd)which is not as simple as
knitr::knit('path/to/your.Rnw')
, in which case the working directory is not automatically changed, and everything is executed in the current R session (in theglobalenv()
by default).Because the Rnw document is always compiled in a new R session, it won't use any objects in your current R session. This is hard to replicate only through the
envir
argument ofknitr::knit()
in the current R session. In particular, you cannot useknitr::knit(envir = new.env())
because althoughnew.env()
is a new environment, it has a default parent environmentparent.frame()
, which is typically theglobalenv()
; you cannot useknitr::knit(envir = emptyenv())
, either, because it is "too clean", and you will have trouble with objects even in the R base package. The only reliable way to replicate what the "Compile PDF" button does is what I said in 1:system2("Rscript", c("-e", shQuote("library(knitr); knit('your.Rnw')"))
, in which caseknit()
uses theglobalenv()
of a new R session.I'm not entirely sure about what RStudio does for the
repos
option. It probably automatically sets this option behind the scenes if it is not set. I think this is a relatively minor issue. You can set it in your.Rprofile
, and I think RStudio should respect your CRAN mirror setting.
Users have always been asking why the Rnw document (or R Markdown documents) are not compiled in the current R session. To us, it basically boils down to which of the following consequences is more surprising or undesired:
- If we knit a document in the current R session, there is no guarantee that your results can be reproduced in another R session (e.g., the next time you open RStudio, or your collaborators open RStudio on their computers).
- If we knit a document in a new R session, users can be surprised that objects are not found (and when they type the object names in the R console, they can see them). This can be surprising, but it is also a good and early reminder that your document probably won't work the next time.
To sum it up, I think:
Knitting in a new R session is better for reproducibilty;
Knitting in the current R session is sometimes more convenient (e.g., you try to knit with different temporary R objects in the current session). Sometimes you also have to knit in the current R session, especially when you are generating PDF reports programmatically, e.g., you use a (for) loop to generate a series of reports. There is no way that you can achieve this only through the "Compile PDF" button (the button is mostly only for a single Rnw document).
BTW, I think what I said above can also apply to the Knit or Knit HTML buttons, but the underlying function is rmarkdown::render()
instead of knitr::knit()
.
Difference between Compile PDF and knit2pdf
The solution: Make sure to set the working directory to the project directory before using knit2pdf, then shorten the "input" path to just the .Rnw file. Thus...
test.R
library("knitr")
diamonds = diamonds[diamonds$cut != "Very Good",]
setwd("/Users/me/Desktop/thing")
knit2pdf(input = "test.Rnw", output = "test.tex")
Knit PDF produced PDF different than Source in RStudio
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.
HTML outputs are different between using knitr in Rstudio & knit2html in command line
Now RStudio uses R Markdown v2 by default, and knitr::knit2html()
still uses R Markdown v1. V2 uses Pandoc and Twitter Bootstrap themes, which are prettier than the style in v1.
Rstudio knit to PDF
I have just installed the new version of RStudio (0.98.932), which prompted me to upgrade a couple of packages (I can't remember which, although I see I have knitr 1.6, markdown 0.7 and rmarkdown 0.2.46). At first I had the same problem; there was only a single 'knit' option on the tool bar. I managed to get the ability to knit to .pdf by adding the following to the head of my .Rmd file.
---
title: "Sample Document"
output: pdf_document
---
Having done that, I now find I do have a drop down menu with options to knit to HTML, PDF and word. There's also a little gear icon that provides access to the R Markdown document options that wasn't there before. I have no idea what the problem was but it seems OK now!
Related Topics
All Levels of a Factor in a Model Matrix in R
How to Put Labels Over Geom_Bar For Each Bar in R With Ggplot2
Custom Legend For Multiple Layer Ggplot
How to Get Week Numbers from Dates
Which Data.Table Syntax For Left Join (One Column) to Prefer
How to Read in Numbers With a Comma as Decimal Separator
Geom_Bar Bars Not Displaying When Specifying Ylim
Find Which Season a Particular Date Belongs To
Dplyr: "Error in N(): Function Should Not Be Called Directly"
Assign Multiple Objects to .Globalenv from Within a Function
Editing Legend (Text) Labels in Ggplot
Gradient of N Colors Ranging from Color 1 and Color 2
Adding a New Column to Each Element in a List of Tables or Data Frames
Select Subset of Columns in Data.Table R