forked from yihui/knitr-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
003-minimal.html
146 lines (134 loc) · 6.58 KB
/
003-minimal.html
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.inline {
background-color: #f7f7f7;
border:solid 1px #B0B0B0;
}
.error {
font-weight: bold;
color: #FF0000;
}
.warning {
font-weight: bold;
}
.message {
font-style: italic;
}
.source, .output, .warning, .error, .message {
padding: 0 1em;
border:solid 1px #F7F7F7;
}
.source {
background-color: #f5f5f5;
}
.left {
text-align: left;
}
.right {
text-align: right;
}
.center {
text-align: center;
}
.hl.num {
color: #AF0F91;
}
.hl.str {
color: #317ECC;
}
.hl.com {
color: #AD95AF;
font-style: italic;
}
.hl.opt {
color: #000000;
}
.hl.std {
color: #585858;
}
.hl.kwa {
color: #295F94;
font-weight: bold;
}
.hl.kwb {
color: #B05A65;
}
.hl.kwc {
color: #55aa55;
}
.hl.kwd {
color: #BC5A65;
font-weight: bold;
}
</style>
<title>A minimal knitr example in HTML</title>
</head>
<body>
<div class="chunk" id="unnamed-chunk-1"><div class="rcode"><div class="source"><pre class="knitr r"><span class="hl kwd">library</span><span class="hl std">(knitr)</span>
<span class="hl std">opts_chunk</span><span class="hl opt">$</span><span class="hl kwd">set</span><span class="hl std">(</span><span class="hl kwc">fig.width</span> <span class="hl std">=</span> <span class="hl num">5</span><span class="hl std">,</span> <span class="hl kwc">fig.height</span> <span class="hl std">=</span> <span class="hl num">5</span><span class="hl std">)</span>
</pre></div>
</div></div>
<p>This is a minimal example which shows <strong>knitr</strong>
working with HTML
pages. See <a href="https://rawgit.com/yihui/knitr-examples/master/003-minimal.html">here</a>
for the output and
<a href="https://github.com/yihui/knitr-examples/blob/master/003-minimal.Rhtml">here</a>
for the source.</p>
<p>Boring stuff as usual:</p>
<div class="chunk" id="unnamed-chunk-2"><div class="rcode"><div class="source"><pre class="knitr r"><span class="hl com">## a simple calculator</span>
<span class="hl num">1</span> <span class="hl opt">+</span> <span class="hl num">1</span>
</pre></div>
<div class="output"><pre class="knitr r">## [1] 2
</pre></div>
<div class="source"><pre class="knitr r"><span class="hl com">## boring random numbers</span>
<span class="hl kwd">set.seed</span><span class="hl std">(</span><span class="hl num">123</span><span class="hl std">)</span>
<span class="hl kwd">rnorm</span><span class="hl std">(</span><span class="hl num">5</span><span class="hl std">)</span>
</pre></div>
<div class="output"><pre class="knitr r">## [1] -0.56048 -0.23018 1.55871 0.07051 0.12929
</pre></div>
</div></div>
<p>We can also produce plots (centered by the
option <code>fig.align='center'</code>):</p>
<div class="chunk" id="html-cars-scatter"><div class="rcode"><div class="source"><pre class="knitr r"><span class="hl kwd">library</span><span class="hl std">(ggplot2)</span>
<span class="hl kwd">plot</span><span class="hl std">(mpg</span> <span class="hl opt">~</span> <span class="hl std">hp, mtcars)</span>
</pre></div>
</div><div class="rimage center"><img src="http://db.yihui.name/knitr-examples/figure/003-minimal-html-cars-scatter-1.png" title="plot of chunk html-cars-scatter" alt="plot of chunk html-cars-scatter" class="plot" /></div><div class="rcode">
<div class="source"><pre class="knitr r"><span class="hl kwd">qplot</span><span class="hl std">(hp, mpg,</span> <span class="hl kwc">data</span> <span class="hl std">= mtcars)</span> <span class="hl opt">+</span> <span class="hl kwd">geom_smooth</span><span class="hl std">()</span>
</pre></div>
</div><div class="rimage center"><img src="http://db.yihui.name/knitr-examples/figure/003-minimal-html-cars-scatter-2.png" title="plot of chunk html-cars-scatter" alt="plot of chunk html-cars-scatter" class="plot" /></div></div>
<p>Errors, messages and warnings can be put into <code>div</code>'s
with different <code>class</code>es:</p>
<div class="chunk" id="unnamed-chunk-3"><div class="rcode"><div class="source"><pre class="knitr r"><span class="hl kwd">sqrt</span><span class="hl std">(</span><span class="hl opt">-</span><span class="hl num">1</span><span class="hl std">)</span> <span class="hl com"># warning</span>
</pre></div>
<div class="warning"><pre class="knitr r">## Warning in sqrt(-1): NaNs produced
</pre></div>
<div class="output"><pre class="knitr r">## [1] NaN
</pre></div>
<div class="source"><pre class="knitr r"><span class="hl kwd">message</span><span class="hl std">(</span><span class="hl str">"knitr says hello to HTML!"</span><span class="hl std">)</span>
</pre></div>
<div class="message"><pre class="knitr r">## knitr says hello to HTML!
</pre></div>
<div class="source"><pre class="knitr r"><span class="hl num">1</span> <span class="hl opt">+</span> <span class="hl str">"a"</span> <span class="hl com"># mission impossible</span>
</pre></div>
<div class="error"><pre class="knitr r">## Error in 1 + "a": non-numeric argument to binary operator
</pre></div>
</div></div>
<p>In the end, let's show off a 3D plot from
the <strong>rgl</strong> package.</p>
<div class="chunk" id="unnamed-chunk-4"><div class="rcode"><div class="source"><pre class="knitr r"><span class="hl kwd">library</span><span class="hl std">(rgl)</span>
<span class="hl std">knit_hooks</span><span class="hl opt">$</span><span class="hl kwd">set</span><span class="hl std">(</span><span class="hl kwc">rgl</span> <span class="hl std">= hook_rgl)</span> <span class="hl com"># set up the hook first</span>
</pre></div>
</div></div>
<div class="chunk" id="fancy-rgl"><div class="rcode"><div class="source"><pre class="knitr r"><span class="hl kwd">demo</span><span class="hl std">(</span><span class="hl str">"bivar"</span><span class="hl std">,</span> <span class="hl kwc">package</span> <span class="hl std">=</span> <span class="hl str">"rgl"</span><span class="hl std">,</span> <span class="hl kwc">echo</span> <span class="hl std">=</span> <span class="hl num">FALSE</span><span class="hl std">)</span>
</pre></div>
<div class="message"><pre class="knitr r">## Loading required package: MASS
</pre></div>
<div class="source"><pre class="knitr r"><span class="hl kwd">par3d</span><span class="hl std">(</span><span class="hl kwc">zoom</span> <span class="hl std">=</span> <span class="hl num">0.7</span><span class="hl std">)</span>
</pre></div>
</div><div class="rimage center"><img src="http://db.yihui.name/knitr-examples/figure/003-minimal-fancy-rgl-1.png" title="plot of chunk fancy-rgl" alt="plot of chunk fancy-rgl" class="plot" /></div></div>
<p>Well, everything seems to be working. Let's ask R what is the
value of π? Of course it is <code class="knitr inline">3.1416</code>.</p>
</body>
</html>