-
Notifications
You must be signed in to change notification settings - Fork 153
/
getting_sophisticated.html
508 lines (435 loc) · 15.3 KB
/
getting_sophisticated.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<title>Time to get sophisticated</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/yeti.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
button.code-folding-btn:focus {
outline: none;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 45px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 50px;
margin-top: -50px;
}
.section h2 {
padding-top: 50px;
margin-top: -50px;
}
.section h3 {
padding-top: 50px;
margin-top: -50px;
}
.section h4 {
padding-top: 50px;
margin-top: -50px;
}
.section h5 {
padding-top: 50px;
margin-top: -50px;
}
.section h6 {
padding-top: 50px;
margin-top: -50px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<div class="container-fluid main-container">
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_').toLowerCase();
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
padding-left: 25px;
text-indent: 0;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">HOME</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="first_observations.html">First Observations</a>
</li>
<li>
<a href="basic_workflow.html">Basic Workflow</a>
</li>
<li>
<a href="getting_sophisticated.html">Getting Sophisticated</a>
</li>
<li>
<a href="your_turn.html">Your Turn</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Time to get sophisticated</h1>
</div>
<p>There are a lot of ways to get sophisticated here, and we’re only covering a few possibilites here. See <a href="http://rmarkdown.rstudio.com/lesson-1.html">RStudio’s R Markdown lessons</a> for other R Markdown capabilities. Plus, R Markdown can render styling from Cascading Style Sheets (CSS) and Hyper Text Markup Language (HTML), which is what non-R Markdown websites use.</p>
<p>So, Googling what you want to do and trying it in your R Markdown files is a good way to learn. Disclaimer: that’s how I’ve learned the following, so there are most definitely better and more elegant ways to do this. But here are some ideas of what you may want to do with your website.</p>
<hr />
<div id="r-markdown-options" class="section level2">
<h2>R Markdown options</h2>
<p>These are options that you can do from the Front Matter.</p>
<p><br></p>
<div id="table-of-contents" class="section level3">
<h3>Table of Contents</h3>
<p>Note that on this page:</p>
<ol style="list-style-type: decimal">
<li>there is a Table of Contents (TOC) there on the left, and</li>
<li>it is floating (i.e. it follows you as you scroll down and expands/collapses nicely as you click)</li>
<li><p>This is all done in the front matter:</p>
<pre><code>output:
html_document:
toc: true
toc_float: true </code></pre>
<!---also note that my numbering in this list renders nicely---></li>
</ol>
<p><br></p>
</div>
<div id="themes" class="section level3">
<h3>Themes</h3>
<p>We can change the website’s theme in <code>_site.yml</code>. Explore options here: <a href="http://rmarkdown.rstudio.com/html_document_format.html#appearance_and_style" class="uri">http://rmarkdown.rstudio.com/html_document_format.html#appearance_and_style</a>:</p>
<blockquote>
<p>Valid themes include “default”, “cerulean”, “journal”, “flatly”, “readable”, “spacelab”, “united”, “cosmo”, “lumen”, “paper”, “sandstone”, “simplex”, and “yeti”.</p>
</blockquote>
<hr />
</div>
</div>
<div id="simple-styling" class="section level2">
<h2>Simple styling</h2>
<div id="images" class="section level3">
<h3>images</h3>
<p>By default, images are displayed left-aligned.</p>
<p><strong>Resize</strong> an image using HTML tags (side note: check out <a href="https://www.rstudio.com/resources/cheatsheets/">RStudio’s R Markdown Cheatsheet</a>):</p>
<p>Here’s a big image:<br />
<code><img src="images/Rmd_cheatsheet.png" width="350px"></code><br />
<img src="images/Rmd_cheatsheet.png" width="350px"></p>
<p>And a small image:<br />
<code><img src="images/Rmd_cheatsheet.png" width="150px"></code><br />
<img src="images/Rmd_cheatsheet.png" width="150px"></p>
<p><strong>Align</strong> an image using HTML tags:</p>
<p>Small image centered:<br />
<code><center><img src="images/Rmd_cheatsheet.png" width="150px"></center></code></p>
<center>
<img src="images/Rmd_cheatsheet.png" width="150px">
</center>
<p>Small image right-aligned:<br />
<code><div align="right"><img src="images/Rmd_cheatsheet.png" width="150px"></div></code></p>
<div align="right">
<img src="images/Rmd_cheatsheet.png" width="150px">
</div>
<p><br></p>
</div>
<div id="columns" class="section level3">
<h3>columns</h3>
<!---From http://stackoverflow.com/questions/31753897/2-column-section-in-r-markdown:
Put in your css file or directly in rmarkdown--->
<style>
.col2 {
columns: 2 200px; /* number of columns and width in pixels*/
-webkit-columns: 2 200px; /* chrome, safari */
-moz-columns: 2 200px; /* firefox */
}
</style>
<div class="col2">
<p><strong>Column 1</strong></p>
<ul>
<li>note <code><div class="col2"></code> and <code></div></code></li>
<li>what beautiful bullet points <br> <br></li>
</ul>
<p><strong>Column 2</strong></p>
<ul>
<li>I have 3 bullet points here</li>
<li>which means to align with the left column</li>
<li>I added a <code><br></code> there</li>
</ul>
</div>
<p><br></p>
<center>
<p><code><center>**Here the text is centered. Here are good resources:**</center></code><br />
<strong>Here the text is centered. Here are good resources:</strong></p>
• <a href="http://www.w3schools.com/tags/">w3schools.com - HTML references</a> • <a href="http://www.w3schools.com/css/">w3schools.com - CSS tutorial</a> •
</center>
<p><br></p>
</div>
<div id="colored-text" class="section level3">
<h3>colored text</h3>
<p><code><font size="3" color="red">Text here!</font></code></p>
<p><font size="3" color="red">This is red text size 3!</font><br />
<font size="2" color="blue">This is blue text size 2!</font><br />
<font face="verdana" color="green">This is green text in verdana!</font></p>
<hr />
</div>
</div>
<div id="r-code" class="section level2">
<h2><code>R</code> code</h2>
<p>You can execute R code each time you build your website. Even if you don’t yet know R, this is a little taste of the types of things you can do. There are two ways you’ll see R code; in-line and in a code chunk. This is from the <a href="http://r4ds.had.co.nz/r-markdown.html">R Markdown chapter in R for Data Science</a>.</p>
<div class="figure">
<img src="images/r4ds_r-markdown.png" />
</div>
<p><br></p>
<div id="in-line" class="section level3">
<h3>in line</h3>
<p>Here is an example inline. The date when I last built the website is 2018-02-22. My favorite number is 13.1415927. There are 19 files and folders in our repo. This is not just text; his is executable R code that is 100% awesome!</p>
<p><br></p>
</div>
<div id="in-a-code-chunk" class="section level3">
<h3>in a code chunk</h3>
<p>And here is an example of a code chunk. Let’s plot the first bit of an <code>iris</code> variable (which is a dataset that comes with base R).</p>
<pre class="r"><code>x <- iris$Sepal.Length[1:15]
plot(x)</code></pre>
<p><img src="getting_sophisticated_files/figure-html/plot%20iris-1.png" width="672" /></p>
</div>
<div id="calling-another-file" class="section level3">
<h3>calling another file</h3>
<div id="r-script" class="section level4">
<h4><code>R</code> script</h4>
<p>You can source an R script. This is a super powerful way to avoid copy-pasting your analyses. For example:</p>
<pre class="r"><code>## source an external script
source("advanced/my_script.R")
## use the calc_gdp() function `my_script.R`
my_data <- calc_gdp(my_country <- 'Slovenia')
ggplot(data = my_data, aes(x = year, y = pop)) +
geom_point() +
labs(title = my_data$country)</code></pre>
<p><img src="getting_sophisticated_files/figure-html/source%20gdp%20script-1.png" width="672" /></p>
<p><br></p>
</div>
<div id="a-specific-r-script-_site.r" class="section level4">
<h4>A specific R script: <code>_site.R</code></h4>
<p>This is something that R Markdown websites will look for; when it exists it will give you ‘global variables’ that will be available for all pages across your site.</p>
<pre class="r"><code>source("_site.R")</code></pre>
<p><br></p>
</div>
<div id="r-markdown-file" class="section level4">
<h4>R Markdown file</h4>
<p>You can insert separate R Markdown files too; text and code will be incorporated seamlessly into this page. This R Markdown file is considered the ‘parent’ file, and the one that is incorporated is the ‘child’ file. In the code chunk below, we’ll say <code>{r, child='advanced/important_example.Rmd'}</code>.</p>
<p>This text comes from a separate R Markdown file – how would you ever know that this is a separate file!</p>
<p>Here is an example of inline R code: if you round 3.1415927 you get 3.</p>
<p>This is the end of this separate and important R Markdown child file.</p>
<p><br></p>
</div>
</div>
</div>
<div id="troubleshooting" class="section level2">
<h2>Troubleshooting</h2>
<p>You’ll get errors as you work; this is how you learn. These are a few things you may encounter:</p>
<ol style="list-style-type: decimal">
<li><p>If when running <code>rmarkdown::render_site()</code> you get this error:</p>
<pre><code>Error in object[seq_len(ile)] :
object of type 'symbol' is not subsettable</code></pre>
<p>Go to <em>Session > Restart R</em> and try again.</p></li>
<li><p>When knitting, if you get this error:</p>
<pre><code>Error in parse_block(g[-1], g[1], params.src) : duplicate label 'setup'
Calls: <Anonymous> ... process_file -> split_file -> lapply -> FUN -> parse_block
Execution halted</code></pre>
<p>Here you have 2 <code>R</code> code chunks named the same thing (in this case ‘setup’), you must change one of them because there cannot be two code chunks with the same name.</p></li>
<li><p>When you get an error message you don’t understand: Copy-paste it into Google.</p></li>
</ol>
<center>
<img src="images/practicalDev_googleErrorMessage.jpg" width="350px">
</center>
</div>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>