Single document containing analysis, code, and results
Sweave (included in R)
New tools
Automatically regenerate documents when code, data, or assumptions change.
Eliminate transposition errors that occur when copying results into documents.
Preserve contextual narrative about why analysis was performed in a certain fashion.
Documentation for the analytic and computational processes from which conclusions are drawn.
Those who receive the results of modern data analysis have limited opportunity to verify the results by direct observation. Users of the analysis have no option but to trust the analysis, and by extension the software that produced it.
This places an obligation on all creators of software to program in such a way that the computations can be understood and trusted. This obligation I label the Prime Directive.
Chambers, Software for Data Analysis: Programming with R
Multi-mode editor (R and TeX aware)
One-click Compile PDF
Two-way sync between source and PDF (SyncTeX)
TeX error log parsing and navigation
Chunk navigation, execution, and code-completion
This code produces an identical result to Knit HTML in RStudio (with no run-time dependency on RStudio):
knit("foo.Rmd")
markdownToHTML("foo.md")
browseURL("foo.html")
Enables use of R Markdown with any editor or IDE
markdown
package for custom publishing
Next-generation re-implementation of Sweave
Adds many features to Sweave including caching, syntax highlighting, code externalization, and new graphics capabilities
Very extensible design
Supports publishing to the web (R Markdown and R HTML)
I do homework, I grade homework, and I saw this:
## option tidy=FALSE
for(k in 1:10){j=cos(sin(k)*k^2)+3;print(j-5)}
Same code, reformatted:
## option tidy=TRUE
for (k in 1:10) {
j = cos(sin(k) * k^2) + 3
print(j - 5)
}
qplot(carat, price, data = diamonds)
just works; no need print()
or fig=TRUE
Compare
> (x = 0)
[1] 0
> x = x + 1
to (default):
(x = 0)
## [1] 0
x = x + 1
You do not appreciate this unless you have been a homework grader.
1 + 1
[1] 2
\begin{verbatim}
[1] 2
\end{verbatim}
<div class="output">
[1] 2
</div>
```[1] 2```
You can control how the source code, normal output, warnings, messages, errors and plots are written in the output document.
knit_hooks$set(source = function(x, options) {
paste("\\begin{DearSource}", x,
"\\end{DearSource}", sep = "")
})
LaTeX, HTML, Markdown and reStructuredText have been supported, and it is straightforward to support other formats.
tikz()
device supported by pgfSweaveanimation::saveLatex
+ R2HTML::RweaveHTML
+ highlight::HighlightWeaveLatex
+ 0.2 * brew + 0.1 * SweaveListingUtils + moreknit()
it (convention over configuration)stitch()
or spin()
In theory you can use any language with knitr, e.g.
<<test-python, engine='python'>>=
x = 'hello, python world!'
print x
print x.split(' ')
@
Contributions needed!
Code + Documentation
<<setup>>=
library(gridExtra)
g = tableGrob(head(iris, 4))
@
<<draw-table, fig.width=convertWidth(grobWidth(g), "in", value=TRUE), fig.height=convertHeight(grobHeight(g), "in", value=TRUE), dev='png', dpi=150>>=
grid.draw(g)
@
Chunk hooks are functions associated with code chunks.
knit_hooks$set(lord = function(before, options, envir) {
library(twitteR)
# Authentication with OAuth here, then
if (!before) {
msg = paste('I have finished the chunk',
options$label, ', my Lord!')
tweet(msg)
}
})
# enable the chunk hook
opts_chunk$set(lord = TRUE)
Questions and comments?
http://www.rstudio.org
https://yihui.org/knitr/