This is a minimal example of using knitr to produce an HTML page from Markdown.
# set global chunk options: images will be 7x5 inches knitr::opts_chunk$set(fig.width=7, fig.height=5) options(digits = 4)
Now we write some code chunks in this markdown file:
x <- 1+1 # a simple calculator set.seed(123) rnorm(5) # boring random numbers
We can also produce plots:
par(mar = c(4, 4, .1, .1)) with(mtcars, { plot(mpg~hp, pch=20, col='darkgray') lines(lowess(hp, mpg)) })
Inline R code is also supported, e.g. the value of x
is r x
, and 2 × π
= r 2*pi
.
LaTeX math as usual: $f(\alpha, \beta) \propto x^{\alpha-1}(1-x)^{\beta-1}$.
You can indent code chunks so they can nest within other environments such as lists.
r
pi * x^2
To compile me, use
library(knitr) knit('knitr-minimal.Rmd')
Markdown is super easy to write. Go to knitr homepage for details.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.