forked from STAT545-UBC/STAT545-UBC-original-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock007_first-use-rmarkdown.html
213 lines (188 loc) · 14.8 KB
/
block007_first-use-rmarkdown.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
<!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>Test drive R Markdown</title>
<script src="libs/jquery-1.11.0/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="libs/bootstrap-2.3.2/css/united.min.css" rel="stylesheet" />
<link href="libs/bootstrap-2.3.2/css/bootstrap-responsive.min.css" rel="stylesheet" />
<script src="libs/bootstrap-2.3.2/js/bootstrap.min.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<link rel="stylesheet"
href="libs/highlight/default.css"
type="text/css" />
<script src="libs/highlight/highlight.js"></script>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs && document.readyState && document.readyState === "complete") {
window.setTimeout(function() {
hljs.initHighlighting();
}, 0);
}
</script>
<link rel="stylesheet" href="libs/local/nav.css" type="text/css" />
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
</style>
<div class="container-fluid main-container">
<header>
<div class="nav">
<a class="nav-logo" href="index.html">
<img src="static/img/stat545-logo-s.png" width="70px" height="70px"/>
</a>
<ul>
<li class="home"><a href="index.html">Home</a></li>
<li class="faq"><a href="faq.html">FAQ</a></li>
<li class="syllabus"><a href="syllabus.html">Syllabus</a></li>
<li class="topics"><a href="topics.html">Topics</a></li>
<li class="people"><a href="people.html">People</a></li>
</ul>
</div>
</header>
<div id="header">
<h1 class="title">Test drive R Markdown</h1>
</div>
<div id="TOC">
<ul>
<li><a href="#overview">Overview</a></li>
<li><a href="#step-0-software-installation-and-configuration">Step 0: Software installation and configuration</a></li>
<li><a href="#step-1-get-ready-to-work">Step 1: Get ready to work</a></li>
<li><a href="#step-2-practice-with-rstudios-boilerplate-r-markdown-document">Step 2: Practice with RStudio’s boilerplate R Markdown document</a></li>
<li><a href="#step-3-save-the-intermediate-markdown">Step 3: Save the intermediate Markdown</a></li>
<li><a href="#step-4-swap-out-the-guts-of-the-document">Step 4: Swap out the “guts” of the document</a></li>
<li><a href="#step-5-develop-your-report">Step 5: Develop your report</a></li>
<li><a href="#step-6-publish-your-report">Step 6: Publish your report</a></li>
<li><a href="#troubleshooting">Troubleshooting</a></li>
</ul>
</div>
<div id="overview" class="section level3">
<h3>Overview</h3>
<p>This describes a hands-on activity where the goal is to author an R Markdown document and render it to HTML. We discuss how to keep the intermediate Markdown file, the figures, and what to commit to Git and push to GitHub.</p>
<p>Here is the official R Markdown documentation:</p>
<ul>
<li><a href="http://rmarkdown.rstudio.com">http://rmarkdown.rstudio.com</a></li>
</ul>
</div>
<div id="step-0-software-installation-and-configuration" class="section level3">
<h3>Step 0: Software installation and configuration</h3>
<p>We assume the following</p>
<ul>
<li>You’ve already <a href="git01_git-install.html">installed Git</a> and (probably) <a href="git02_git-clients.html">a Git client</a>.</li>
<li>You’ve already <a href="cm001_course-intro-sw-install-account-signup.html#register-a-github-account">registered a free GitHub account</a>.</li>
<li>You’ve already <a href="block000_r-rstudio-install.html">installed R and RStudio</a>.</li>
<li>You’ve <a href="git05_first-use-git-rstudio.html">tested your installation and configuration of Git, GitHub, and RStudio</a></li>
<li>You’ve completed <a href="hw01_edit-README.html">Homework 01</a> or an equivalent exercise to truly test all of the above and to introduce you to Markdown.</li>
</ul>
</div>
<div id="step-1-get-ready-to-work" class="section level3">
<h3>Step 1: Get ready to work</h3>
<p>Launch RStudio, probably in the Project that corresponds to the repository where you are keeping all STAT 545 coursework. Make sure the workspace is clean and you’ve launched a fresh R process. Make sure the working directory is sensible.</p>
</div>
<div id="step-2-practice-with-rstudios-boilerplate-r-markdown-document" class="section level3">
<h3>Step 2: Practice with RStudio’s boilerplate R Markdown document</h3>
<p>I am modelling “walk before you run” here. It is best, especially for novices, to increase complexity in small increments. We will test our system’s ability to render the <a href="http://en.wikipedia.org/wiki/%22Hello,_world!%22_program">“hello world”</a> of R Markdown documents before we muddy the waters with our own, probably buggy, documents.</p>
<p>Do this: File > New File > R Markdown …</p>
<ul>
<li>Give it an informative title. This will appear in the document but does not necessarily have anything to do with the file’s name. But the title and filename should be similar! The title is for human eyeballs, so it can contain spaces and punctuation. The filename is for humans and computers, so it should have similar words in it but no spaces and no punctuation.</li>
<li>Accept the default Author or edit if you wish.</li>
<li>Accept the default output format of HTML.</li>
<li>Click OK.</li>
</ul>
<p>Save this document to a reasonable filename and location. The filename should end in <code>.rmd</code> or <code>.Rmd</code>. I highly recommend saving in the top-level of the directory that is also also a Git repository for your coursework and that is also an RStudio project and that is also current working directory.</p>
<p>Click on “Knit HTML” or do File > Knit. RStudio should display a preview of the resulting HTML. Also look at the file browser (which should be pointed at the directory where you saved the <code>.rmd</code> file). You should see the R Markdown document, i.e. <code>foo.rmd</code> AND the resulting HTML <code>foo.html</code>.</p>
<p>Congratulations, you’ve just made your first reproducible report with R Markdown.</p>
</div>
<div id="step-3-save-the-intermediate-markdown" class="section level3">
<h3>Step 3: Save the intermediate Markdown</h3>
<p><em>This is directly related to eventual publishing on GitHub. If that does not apply to you, skip this step.</em></p>
<p>It will have nice side effects on GitHub if we save the intermediate Markdown file that is produced when compiling to HTML. The magical process is like so: <code>foo.rmd --> foo.md --> foo.html</code>. By default RStudio discards <code>foo.md</code> but it’s easy to request that it be kept. This is one of the many things we can control in the YAML frontmatter – the text at the top of your file between leading and trailing lines of <code>---</code>. Two approaches:</p>
<ul>
<li><p>RStudio GUI: click on the “gear” in the top bar of the source editor, near the “Knit HTML” button. Go to the Advanced tab and check “Keep markdown source file.”</p></li>
<li><p>“By hand:” Make your YAML frontmatter look something like this:</p></li>
</ul>
<pre class="sh"><code> ---
title: "Something fascinating"
author: "Jenny Bryan"
date: "10 September, 2014"
output:
html_document:
keep_md: true
--- </code></pre>
<p>Save! Render via “Knit HTML” button.</p>
<p>Now revisit the file browser. In addition to <code>foo.rmd</code> and <code>foo.html</code>, you should now see <code>foo.md</code> and a directory <code>foo_files</code>, where any figures created by the document will live.</p>
</div>
<div id="step-4-swap-out-the-guts-of-the-document" class="section level3">
<h3>Step 4: Swap out the “guts” of the document</h3>
<p>Select everything but the YAML frontmatter and … delete it!</p>
<p>Write a single English sentence.</p>
<p>Insert an empty R chunk, via the “Chunk” menu in upper right of source editor or with corresponding keyboard shortcut.</p>
<pre><code>```{r}
## insert your brilliant WORKING code here
```</code></pre>
<p>Insert 1 to 3 lines of functioning code that begin the task at hand. “Walk through” and run those lines using the “Run” button or the corresponding keyboard shortcut. You MUST make sure your code actually works!</p>
<p>Satisfied? Save!</p>
<p>Now render the whole document via “Knit HTML.” Voilà!</p>
</div>
<div id="step-5-develop-your-report" class="section level3">
<h3>Step 5: Develop your report</h3>
<p>In this incremental manner, develop your report. Add code to this chunk. Refine it. Add new chunks. Go crazy! But keep running the code “manually” to make sure it works. If it doesn’t work with you babysitting it, I can guarantee you it will fail, in a more spectacular and cryptic way, when run at arms-length via “Knit HTML” or <code>rmarkdown::render()</code>. Clean out your workspace and restart R and re-run everything periodically, if things get weird. There are lots of chunk menu items and keyboard shortcuts to accelerate this workflow. Render the whole document often to catch errors when they’re easy to pinpoint and fix. Save often and commit every time you reach a point that you’d like as a “fall back” position.</p>
<p>You’ll develop your own mojo soon, but this should give you your first successful R Markdown experience.</p>
</div>
<div id="step-6-publish-your-report" class="section level3">
<h3>Step 6: Publish your report</h3>
<p>Since we are pushing coursework to GitHub anyway, I focus on how that delivers decent web publishing for “free.”</p>
<p>Markdown documents get special treatment on GitHub: when you visit one in a web browser, instead of seeing the raw Markdown, by default you see a preview of how it will look when rendered to proper HTML. This is why, in Step 3, we alter the YAML to request retention of the intermediate Markdown file. If there are R chunks that make figures, <code>keep_md: true</code> will also cause those figure files to be left behind in a sensibly named sub-directory. If you commit and push <code>foo.md</code> and everything inside <code>foo_files</code>, then anyone with permission to view your GitHub repo can see a decent-looking version of your report.</p>
<p>This is (sort of) another example of keeping things machine- and human-readable. By making <code>foo.rmd</code> available, others can see and run your <strong>actual code</strong>. By sharing <code>foo.md</code> and/or <code>foo.html</code>, others can casually browse your end product and decide if they even want to run your code.</p>
<p>HTML files, such as <code>foo.html</code>, are not immediately useful on GitHub (though your local versions are easily viewable). Visit one and you’ll see the raw HTML. Yuck. But there is a way to get a preview: <a href="http://htmlpreview.github.io">http://htmlpreview.github.io</a>. Expect some token/authentication pain with HTML files inside private repos. When it becomes vital to see proper HTML in its full glory, it’s time to use a more sophisticated web publishing strategy.</p>
</div>
<div id="troubleshooting" class="section level3">
<h3>Troubleshooting</h3>
<p><strong>Make sure RStudio and the <code>rmarkdown</code> package (and its dependencies) are up-to-date.</strong> In case of catastrophic failure to render R Markdown, consider that your software may be too old. R Markdown has been developing rapidly (written 2014-09), so you need a very current version of RStudio and <code>rmarkdown</code> to enjoy all the goodies we describe here.</p>
<p><strong>Get rid of your <code>.Rprofile</code></strong>, at least temporarily. I have found that a “mature” <code>.Rprofile</code> that has accumulated haphazardly over the years can cause trouble. Specifically, if you’ve got anything in there relating to <code>knitr</code>, <code>markdown</code>, <code>rmarkdown</code> and RStudio stuff, it may be preventing the installation or usage of the most recent goodies (see above). Comment the whole file out or rename it something else and relaunch or reinstall RStudio.</p>
<p><strong>Insert a chunk in your <code>.rmd</code> document so that it renders even when there are errors.</strong> Some errors are easier to diagnose if you can execute specific R statements during rendering and leave more evidence behind for forensic examination. Put <a href="https://gist.github.com/jennybc/679ea117b61c9126034a">this chunk</a>:</p>
<pre><code>```{r setup, include = FALSE, cache = FALSE}
knitr::opts_chunk$set(error = TRUE)
```</code></pre>
<p>near the top of your R Markdown document if you want to soldier on through errors, i.e. turn <code>foo.rmd</code> into <code>foo.md</code> and/or <code>foo.html</code> no matter what. This is also helpful if you are writing a tutorial and want to demo code that throws an error.</p>
<p><strong>Check your working directory.</strong> It’s going to break your heart as you learn how often your mistakes are really mundane and basic. Ask me how I know. When things go wrong consider:</p>
<ul>
<li>What is the working directory?</li>
<li>Is that file I want to read/write actually where I think it is?</li>
</ul>
<p>Drop these commands into R chunks to check the above: <code>getwd()</code> will display working directory at <strong>run time</strong>. If you monkeyed around with working directory with, e.g., the mouse, maybe it’s set to one place for your interactive development and another when “Knit HTML” takes over. <code>list.files()</code> is also handy for brute force checking of which files are lying around within arm’s reach.</p>
<p><a href="https://github.com/yihui/knitr/blob/master/FAQ.md">Don’t try to change working directory within an R Markdown document</a>. Just don’t. And don’t be in a hurry to create a complicated sub-directory structure. RStudio/<code>knitr</code>/<code>rmarkdown</code> (which bring you the “Knit HTML” button) are rather opinionated about the working directory being set to the <code>.rmd</code> file’s location and about all files living together in one big happy directory. This can all be worked around. But not today.</p>
</div>
<div class="footer">
This work is licensed under the <a href="http://creativecommons.org/licenses/by-nc/3.0/">CC BY-NC 3.0 Creative Commons License</a>.
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
$(document).ready(function () {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>