The knitr package is an alternative tool to Sweave based on a different design with more features. This document is not an introduction, but only serves as a placeholder to guide you to the real manuals, which are available on the package website https://yihui.org/knitr/ (e.g. the main manual and the graphics manual ), and remember to read the help pages of functions in this package. There is a book “Dynamic Documents with R and knitr” for this package, too.
Below are code chunk examples:
options(digits = 4)
rnorm(20)
#> [1] 1.0362 -2.1393 -0.1816 1.8472 0.7833 -1.4270 2.4287 0.4748 1.8223
#> [10] -0.2965 0.5717 1.1474 0.5671 0.8176 -0.8424 1.1289 0.4271 -0.6813
#> [19] 3.0578 -0.5815
fit = lm(dist ~ speed, data = cars)
b = coef(fit)
| Estimate | Std. Error | t value | Pr(>|t|) | |
|---|---|---|---|---|
| (Intercept) | -17.579 | 6.758 | -2.601 | 0.012 |
| speed | 3.932 | 0.416 | 9.464 | 0.000 |
The fitted regression equation is \(Y=-17.6+3.93x\).
par(mar=c(4, 4, 1, .1))
plot(cars, pch = 20)
abline(fit, col = 'red')
1 A scatterplot with a regression line.
Xie Y (2026). knitr: A General-Purpose Package for Dynamic Report Generation in R. R package version 1.52, https://yihui.org/knitr/.
Xie Y (2015). Dynamic Documents with R and knitr, 2nd edition. Chapman and Hall/CRC, Boca Raton, Florida. ISBN 978-1498716963, https://yihui.org/knitr/.
Xie Y (2014). “knitr: A Comprehensive Tool for Reproducible Research in R.” In Stodden V, Leisch F, Peng RD (eds.), Implementing Reproducible Computational Research. Chapman and Hall/CRC. ISBN 978-1466561595.