62 lines
1.7 KiB
Plaintext
Raw Normal View History

2025-01-12 00:52:51 +08:00
---
title: R Markdown (v1) Vignettes with knitr
author: Yihui Xie
date: "`{r} Sys.Date()`"
output:
litedown::html_format:
meta:
css: ["@default"]
---
<!--
%\VignetteEngine{litedown::vignette}
%\VignetteIndexEntry{An R Markdown Vignette with knitr}
-->
Before R 3.0.0, only Sweave/PDF vignettes were supported in R. Markdown is gaining popularity over the years due to its simplicity, and R 3.0.0 starts to support package vignettes written in R Markdown.
> Please note this example is for [R Markdown v1](https://cran.r-project.org/package=markdown) only. If you use [R Markdown v2](https://rmarkdown.rstudio.com), you should use the vignette engine `knitr::rmarkdown` instead of `knitr::knitr`.
## Package vignettes
To enable Markdown vignettes in an R package, you need to
- add `*.Rmd` files under the `vignettes` directory
- add `VignetteBuilder: knitr` to the `DESCRIPTION` file
- specify the vignette engine `\VignetteEngine{knitr::knitr}` in the `Rmd` files (inside HTML comments)
## View vignettes
And R will load the **knitr** package to build these vignettes to HTML files, and you can see them when you open the HTML help:
```{r eval=FALSE}
help(package = 'YourPackage', help_type = 'html')
# or see a standalone list of vignettes
browseVignettes('YourPackage')
```
## Examples
Below are some code chunks as examples.
```{r hello, results='asis'}
if (TRUE) cat('_hello_ **markdown**!', '\n')
```
Normally you do not need any chunk options.
```{r test, collapse=TRUE}
1+1
10:1
rnorm(5)^2
strsplit('hello, markdown vignettes', '')
```
Feel free to draw beautiful plots and write math $P(X>x)=\alpha/2$.
```{r}
n=300; set.seed(123)
par(mar=c(4,4,.1,.1))
plot(rnorm(n), rnorm(n), pch=21, cex=5*runif(n), col='white', bg='gray')
```