- https://yihui.org
- first language: Chinese
- second language: R (10 years, authored a few R packages)
- third language: English
- graduated from Iowa State U
- working for RStudio
06/30/2014 @ useR! 2014
This is an intermediate level tutorial on dynamic reporting with R and the knitr package (https://yihui.org/knitr). It covers the basic idea of literate programming as well as its role in reproducible research.
A variety of document formats will be introduced, including R LaTeX (.Rnw) and R Markdown (.Rmd). We will show useful features of knitr, such as creating tables and plots from data, caching, and cross references.
We will also provide examples of advanced features such as chunk hooks, and calling foreign languages (shell scripts, Python, C++, Julia, …).
Please do that again! (sorry we made a mistake in the data, want to change a parameter, and yada yada)
i.e. computing languages + authoring languages
We built a linear regression model. ```{r} fit <- lm(dist ~ speed, data = cars) b <- coef(fit) plot(fit) ``` The slope of the regression is `r b[1]`.
an example
install.packages('knitr')
)if (!require("knitr")) install.packages("knitr") library(knitr) knit("your-document.Rmd") # compiles a document
internally converted to R Markdown/LaTeX/…
#' today I built a model fit = lm(dist ~ speed, data = cars) #' and I got the slope `r coef(fit)[2]` #+ dist-speed, fig.width=5, fig.height=4 plot(cars) abline(fit)
demo: knitr::spin('05-knitr-spin.R')
knitr::stitch('06-stitch-test.R')
<<label>>
ref.label
child
knit_child()
additional tasks attached to chunk options
knit_hooks$set(opt_name = function(before, options) { if (before) do_something() else do_something_else() })
set the chunk option opt_name
to a non-NULL value to activate the chunk hook
engine
Julia (experimental)
names(knitr::knit_engines$get())
## [1] "awk" "bash" "coffee" "gawk" "haskell" ## [6] "perl" "python" "Rscript" "ruby" "sas" ## [11] "scala" "sed" "sh" "zsh" "highlight" ## [16] "Rcpp" "tikz" "dot" "c" "fortran" ## [21] "asy" "cat" "asis"
demo: 12-python.Rmd, 14-julia.Rmd
data
, R
, man
, tests
, demo
, src
, vignettes
VignetteBuilder: knitr
in DESCRIPTION\VignetteEngine{knitr::knitr}
in vignettes# see list of vignettes help(package = "knitr", help_type = "html") # Docco Classic Style
\section{Introduction} We did a \emph{cool} study, and our main findings: \begin{enumerate} \item You can never remember how to escape backslashes. \item A dollar sign is \$, an ampersand \&, and a \textbackslash{}. \item How about ~? Use $\sim$. \end{enumerate}
# Introduction We did a _cool_ study, and our main findings: 1. You do not need to remember a lot of rules. 2. A dollar sign is $, an ampersand is &, and a backslash \. 3. A tilde is ~. Write content instead of markup languages.
LaTeX
Markdown
You have done the hard work of research, data collection, and analysis, etc. We hope the last step can be easier.