forked from yihui/knitr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knitr-markdown.Rmd
59 lines (41 loc) · 1.77 KB
/
knitr-markdown.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!--
%\VignetteEngine{knitr::knitr}
%\VignetteIndexEntry{An R Markdown Vignette with knitr}
-->
This is an example of Markdown vignettes in R. 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](http://www.rstudio.com/ide/docs/authoring/using_markdown).
## 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')
```
## Examples
Below are some code chunks as examples.
```{r hello, results='asis'}
cat('_hello_ **markdown**!', '\n')
```
Normally you do not need any chunk options.
```{r}
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')
```
You can use your own CSS file instead of the built-in style in the **markdown** package -- just set the option `markdown.HTML.stylesheet`, e.g.
```{r css, eval=FALSE}
options(markdown.HTML.stylesheet = 'path/to/a/custom/style.css')
```
For this vignette, I use the `markdown.HTML.header` option to modify a few CSS rules in the default CSS style:
```{r header}
options(markdown.HTML.header = system.file('misc', 'vignette.css', package='knitr'))
```