opts_chunk$set(fig.width = 5, fig.height = 5)
This is a minimal example which shows knitr working with HTML pages. See here for the output and here for the source.
Boring stuff as usual:
## a simple calculator
1 + 1
## [1] 2
## boring random numbers set.seed(123) rnorm(5)
## [1] -0.56048 -0.23018 1.55871 0.07051 0.12929
We can also produce plots (centered by the
option fig.align='center'
):
library(ggplot2) plot(mpg ~ hp, mtcars)
qplot(hp, mpg, data = mtcars) + geom_smooth()
Errors, messages and warnings can be put into div
's
with different class
es:
sqrt(-1) # warning
## Warning: NaNs produced
## [1] NaN
message("knitr says hello to HTML!")
1 + "a" # mission impossible
## Error: non-numeric argument to binary operator
In the end, let's show off a 3D plot from the rgl package.
knit_hooks$set(rgl = hook_rgl) # set up the hook first
if (require("rgl")) { demo("bivar", package = "rgl", echo = FALSE) par3d(zoom = 0.7) }
Well, everything seems to be working. Let's ask R what is the
value of π? Of course it is 3.1416
.