Skip to content

Commit

Permalink
presentation writing in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomUK committed Sep 23, 2023
1 parent bcf5f81 commit f1734d1
Show file tree
Hide file tree
Showing 3 changed files with 508 additions and 7 deletions.
291 changes: 287 additions & 4 deletions 2023-NHSR-Conf-Presentation.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,70 @@
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ color: #003b4f; background-color: #f1f3f5; }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span { color: #003b4f; } /* Normal */
code span.al { color: #ad0000; } /* Alert */
code span.an { color: #5e5e5e; } /* Annotation */
code span.at { color: #657422; } /* Attribute */
code span.bn { color: #ad0000; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #003b4f; } /* ControlFlow */
code span.ch { color: #20794d; } /* Char */
code span.cn { color: #8f5902; } /* Constant */
code span.co { color: #5e5e5e; } /* Comment */
code span.cv { color: #5e5e5e; font-style: italic; } /* CommentVar */
code span.do { color: #5e5e5e; font-style: italic; } /* Documentation */
code span.dt { color: #ad0000; } /* DataType */
code span.dv { color: #ad0000; } /* DecVal */
code span.er { color: #ad0000; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #ad0000; } /* Float */
code span.fu { color: #4758ab; } /* Function */
code span.im { color: #00769e; } /* Import */
code span.in { color: #5e5e5e; } /* Information */
code span.kw { color: #003b4f; } /* Keyword */
code span.op { color: #5e5e5e; } /* Operator */
code span.ot { color: #003b4f; } /* Other */
code span.pp { color: #ad0000; } /* Preprocessor */
code span.sc { color: #5e5e5e; } /* SpecialChar */
code span.ss { color: #20794d; } /* SpecialString */
code span.st { color: #20794d; } /* String */
code span.va { color: #111111; } /* Variable */
code span.vs { color: #20794d; } /* VerbatimString */
code span.wa { color: #5e5e5e; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="2023-NHSR-Conf-Presentation_files/libs/revealjs/dist/theme/quarto.css" id="theme">
<link href="2023-NHSR-Conf-Presentation_files/libs/revealjs/plugin/quarto-line-highlight/line-highlight.css" rel="stylesheet">
Expand Down Expand Up @@ -335,14 +399,233 @@ <h1 class="title">2023-NHSR-Conf-Presentation</h1>
</div>

</section>
<section id="quarto" class="slide level2">
<h2>Quarto</h2>
<p>Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <a href="https://quarto.org" class="uri">https://quarto.org</a>.</p>
<section class="slide level2">

<style>
.highlighted {
background-color: yellow;
}
</style>
</section>
<section id="purrr" class="slide level2">
<h2>purrr</h2>

<img data-src="purrr_logo.png" class="r-stretch quarto-figure-center"><p>Ahhh, cute cat…</p>
</section>
<section id="scary-functional-programming" class="slide level2">
<h2>Scary functional programming</h2>

<img data-src="purrr_logo.png" class="r-stretch quarto-figure-center"><p>Run away now!</p>
</section>
<section id="aims" class="slide level2">
<h2>Aims</h2>
<ul>
<li class="fragment">Not scary!</li>
</ul>
<div class="fragment">
<div>
<ul>
<li>Practical intro to purrr</li>
<li>Focus on a “mental model” which helped me</li>
<li>Simple examples</li>
</ul>
</div>
</div>
<div class="fragment">
<p>Functional programming is something we can save for another day (and another speaker!)</p>
</div>
</section>
<section id="the-package-website" class="slide level2">
<h2>The package website</h2>
<p>From the tidyverse documentation:</p>
<blockquote>
<p>“the best place to start is the family of map() functions which allow you to replace many for loops with code that is both more succinct and easier to read.”</p>
</blockquote>
<p>and:</p>
<blockquote>
<p>“The best place to learn about the map() functions is the <a href="https://r4ds.had.co.nz/iteration.html">iteration chapter in R for data science.</a></p>
</blockquote>
<aside class="notes">
<p>Purrr is part of the tidyverse, and both of these comments are true.</p>
<p>The first hints at the fact that once you grasp the map() function, you’ll already be able to use it’s close relations in helpful ways.</p>
<p>The second says the best place to learn is Hadley’s book R for Data Science. In the meantime I hope this presentation helps!</p>
<style type="text/css">
span.MJX_Assistive_MathML {
position:absolute!important;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0 0 0!important;
border: 0!important;
height: 1px!important;
width: 1px!important;
overflow: hidden!important;
display:block!important;
}</style></aside>
</section>
<section id="looping" class="slide level2">
<h2>Looping</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1"></a><span class="co"># we need a vector to iterate over</span></span>
<span id="cb1-2"><a href="#cb1-2"></a>food <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">"croissant"</span>, <span class="st">"baked potato"</span>)</span>
<span id="cb1-3"><a href="#cb1-3"></a></span>
<span id="cb1-4"><a href="#cb1-4"></a><span class="co"># we need to create a place to put the results (the right size)</span></span>
<span id="cb1-5"><a href="#cb1-5"></a>result <span class="ot">&lt;-</span> <span class="fu">vector</span>(<span class="st">"character"</span>, <span class="fu">length</span>(food))</span>
<span id="cb1-6"><a href="#cb1-6"></a></span>
<span id="cb1-7"><a href="#cb1-7"></a><span class="co"># here's the loop</span></span>
<span id="cb1-8"><a href="#cb1-8"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="fu">seq_along</span>(food)){</span>
<span id="cb1-9"><a href="#cb1-9"></a> </span>
<span id="cb1-10"><a href="#cb1-10"></a> result[[i]] <span class="ot">&lt;-</span> <span class="fu">paste</span>(<span class="st">"Hot"</span>, food[[i]])</span>
<span id="cb1-11"><a href="#cb1-11"></a> </span>
<span id="cb1-12"><a href="#cb1-12"></a>}</span>
<span id="cb1-13"><a href="#cb1-13"></a></span>
<span id="cb1-14"><a href="#cb1-14"></a>result</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell fragment">
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Hot croissant" "Hot baked potato"</code></pre>
</div>
</div>
</section>
<section id="mapping" class="slide level2">
<h2>Mapping</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1"></a><span class="co"># we still need a vector to iterate over</span></span>
<span id="cb3-2"><a href="#cb3-2"></a>food <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">"croissant"</span>, <span class="st">"baked potato"</span>)</span>
<span id="cb3-3"><a href="#cb3-3"></a></span>
<span id="cb3-4"><a href="#cb3-4"></a><span class="co"># we create a function to do the "work"</span></span>
<span id="cb3-5"><a href="#cb3-5"></a>heat_the_food <span class="ot">&lt;-</span> <span class="cf">function</span>(food){</span>
<span id="cb3-6"><a href="#cb3-6"></a> </span>
<span id="cb3-7"><a href="#cb3-7"></a> <span class="fu">paste</span>(<span class="st">"Hot"</span>, food)</span>
<span id="cb3-8"><a href="#cb3-8"></a></span>
<span id="cb3-9"><a href="#cb3-9"></a>}</span>
<span id="cb3-10"><a href="#cb3-10"></a></span>
<span id="cb3-11"><a href="#cb3-11"></a><span class="co"># here's the loop</span></span>
<span id="cb3-12"><a href="#cb3-12"></a>result <span class="ot">&lt;-</span> purrr<span class="sc">::</span><span class="fu">map</span>(food, heat_the_food)</span>
<span id="cb3-13"><a href="#cb3-13"></a></span>
<span id="cb3-14"><a href="#cb3-14"></a>result</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell fragment">
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2</code></pre>
<pre><code>[[1]]
[1] "Hot croissant"

[[2]]
[1] "Hot baked potato"</code></pre>
</div>
</div>
<aside class="notes">
<p>Ignore for a moment the shape of the result. In the loop example we got a vector back. In the map example we have a list. We’ll come back to that shortly.</p>
<p>But even with that difference you’re sceptical. Tom, you’re short-changing us. We’re happy with loops. 14 lines of code. You’re talking about map() being simpler, and you still took 14 lines.</p>
<style type="text/css">
span.MJX_Assistive_MathML {
position:absolute!important;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0 0 0!important;
border: 0!important;
height: 1px!important;
width: 1px!important;
overflow: hidden!important;
display:block!important;
}</style></aside>
</section>
<section id="comparison" class="slide level2" data-auto-animate="true">
<h2 data-id="quarto-animate-title">Comparison</h2>
<p>loop</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode numberSource r number-lines code-with-copy" data-id="quarto-animate-code"><code class="sourceCode r hljs"><span id="cb5-1" class="hljs-ln-code"><a href="#cb5-1"></a></span>
<span id="cb5-2" class="hljs-ln-code"><a href="#cb5-2"></a><span class="co"># we need a vector to iterate over</span></span>
<span id="cb5-3" class="hljs-ln-code"><a href="#cb5-3"></a>food <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">"croissant"</span>, <span class="st">"baked potato"</span>)</span>
<span id="cb5-4" class="hljs-ln-code"><a href="#cb5-4"></a></span>
<span id="cb5-5" class="hljs-ln-code"><a href="#cb5-5"></a><span class="co"># we need to create a place to put the results (the right size)</span></span>
<span id="cb5-6" class="hljs-ln-code"><a href="#cb5-6"></a>result <span class="ot">&lt;-</span> <span class="fu">vector</span>(<span class="st">"character"</span>, <span class="fu">length</span>(food))</span>
<span id="cb5-7" class="hljs-ln-code"><a href="#cb5-7"></a></span>
<span id="cb5-8" class="hljs-ln-code"><a href="#cb5-8"></a><span class="co"># here's the loop</span></span>
<span id="cb5-9" class="hljs-ln-code"><a href="#cb5-9"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="fu">seq_along</span>(food)){</span>
<span id="cb5-10" class="hljs-ln-code"><a href="#cb5-10"></a> </span>
<span id="cb5-11" class="hljs-ln-code"><a href="#cb5-11"></a> result[[i]] <span class="ot">&lt;-</span> <span class="fu">paste</span>(<span class="st">"Hot"</span>, food[[i]])</span>
<span id="cb5-12" class="hljs-ln-code"><a href="#cb5-12"></a> </span>
<span id="cb5-13" class="hljs-ln-code"><a href="#cb5-13"></a>}</span>
<span id="cb5-14" class="hljs-ln-code"><a href="#cb5-14"></a></span>
<span id="cb5-15" class="hljs-ln-code"><a href="#cb5-15"></a>result</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="comparison-1" class="slide level2" data-auto-animate="true">
<h2 data-id="quarto-animate-title">Comparison</h2>
<p>map</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode numberSource r number-lines code-with-copy" data-id="quarto-animate-code"><code class="sourceCode r hljs"><span id="cb6-1" class="hljs-ln-code"><a href="#cb6-1"></a></span>
<span id="cb6-2" class="hljs-ln-code"><a href="#cb6-2"></a><span class="co"># we need a vector to iterate over</span></span>
<span id="cb6-3" class="hljs-ln-code"><a href="#cb6-3"></a>food <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">"croissant"</span>, <span class="st">"baked potato"</span>)</span>
<span id="cb6-4" class="hljs-ln-code"><a href="#cb6-4"></a></span>
<span id="cb6-5" class="hljs-ln-code"><a href="#cb6-5"></a><span class="co"># we create a function to do the "work"</span></span>
<span id="cb6-6" class="hljs-ln-code"><a href="#cb6-6"></a>heat_the_food <span class="ot">&lt;-</span> <span class="cf">function</span>(food){</span>
<span id="cb6-7" class="hljs-ln-code"><a href="#cb6-7"></a> </span>
<span id="cb6-8" class="hljs-ln-code"><a href="#cb6-8"></a> <span class="fu">paste</span>(<span class="st">"Hot"</span>, food)</span>
<span id="cb6-9" class="hljs-ln-code"><a href="#cb6-9"></a></span>
<span id="cb6-10" class="hljs-ln-code"><a href="#cb6-10"></a>}</span>
<span id="cb6-11" class="hljs-ln-code"><a href="#cb6-11"></a></span>
<span id="cb6-12" class="hljs-ln-code"><a href="#cb6-12"></a><span class="co"># here's the loop</span></span>
<span id="cb6-13" class="hljs-ln-code"><a href="#cb6-13"></a>result <span class="ot">&lt;-</span> purrr<span class="sc">::</span><span class="fu">map</span>(food, heat_the_food)</span>
<span id="cb6-14" class="hljs-ln-code"><a href="#cb6-14"></a></span>
<span id="cb6-15" class="hljs-ln-code"><a href="#cb6-15"></a>result</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="comparison-2" class="slide level2" data-auto-animate="true">
<h2 data-id="quarto-animate-title">Comparison</h2>
<p>loop</p>
<div class="sourceCode" id="cb7" data-code-line-numbers="|6|9|11"><pre class="sourceCode numberSource r number-lines code-with-copy" data-id="quarto-animate-code"><code class="sourceCode r hljs"><span id="cb7-1" class="hljs-ln-code"><a href="#cb7-1"></a></span>
<span id="cb7-2" class="hljs-ln-code"><a href="#cb7-2"></a><span class="co"># we need a vector to iterate over</span></span>
<span id="cb7-3" class="hljs-ln-code"><a href="#cb7-3"></a>food <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">"croissant"</span>, <span class="st">"baked potato"</span>)</span>
<span id="cb7-4" class="hljs-ln-code"><a href="#cb7-4"></a></span>
<span id="cb7-5" class="hljs-ln-code"><a href="#cb7-5"></a><span class="co"># we need to create a place to put the results (the right size)</span></span>
<span id="cb7-6" class="hljs-ln-code"><a href="#cb7-6"></a>result <span class="ot">&lt;-</span> <span class="fu">vector</span>(<span class="st">"character"</span>, <span class="fu">length</span>(food))</span>
<span id="cb7-7" class="hljs-ln-code"><a href="#cb7-7"></a></span>
<span id="cb7-8" class="hljs-ln-code"><a href="#cb7-8"></a><span class="co"># here's the loop</span></span>
<span id="cb7-9" class="hljs-ln-code"><a href="#cb7-9"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="fu">seq_along</span>(food)){</span>
<span id="cb7-10" class="hljs-ln-code"><a href="#cb7-10"></a> </span>
<span id="cb7-11" class="hljs-ln-code"><a href="#cb7-11"></a> result[[i]] <span class="ot">&lt;-</span> <span class="fu">paste</span>(<span class="st">"Hot"</span>, food[[i]])</span>
<span id="cb7-12" class="hljs-ln-code"><a href="#cb7-12"></a> </span>
<span id="cb7-13" class="hljs-ln-code"><a href="#cb7-13"></a>}</span>
<span id="cb7-14" class="hljs-ln-code"><a href="#cb7-14"></a></span>
<span id="cb7-15" class="hljs-ln-code"><a href="#cb7-15"></a>result</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>There is more “boilerplate” code in a loop, and code explanations end up in comments (or don’t exist)</p>
</section>
<section id="comparison-3" class="slide level2" data-auto-animate="true">
<h2 data-id="quarto-animate-title">Comparison</h2>
<p>map</p>
<div class="sourceCode" id="cb8" data-code-line-numbers="|6|8|13"><pre class="sourceCode numberSource r number-lines code-with-copy" data-id="quarto-animate-code"><code class="sourceCode r hljs"><span id="cb8-1" class="hljs-ln-code"><a href="#cb8-1"></a></span>
<span id="cb8-2" class="hljs-ln-code"><a href="#cb8-2"></a><span class="co"># we need a vector to iterate over</span></span>
<span id="cb8-3" class="hljs-ln-code"><a href="#cb8-3"></a>food <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">"croissant"</span>, <span class="st">"baked potato"</span>)</span>
<span id="cb8-4" class="hljs-ln-code"><a href="#cb8-4"></a></span>
<span id="cb8-5" class="hljs-ln-code"><a href="#cb8-5"></a><span class="co"># we create a function to do the "work"</span></span>
<span id="cb8-6" class="hljs-ln-code"><a href="#cb8-6"></a>heat_the_food <span class="ot">&lt;-</span> <span class="cf">function</span>(food){</span>
<span id="cb8-7" class="hljs-ln-code"><a href="#cb8-7"></a> </span>
<span id="cb8-8" class="hljs-ln-code"><a href="#cb8-8"></a> <span class="fu">paste</span>(<span class="st">"Hot"</span>, food)</span>
<span id="cb8-9" class="hljs-ln-code"><a href="#cb8-9"></a></span>
<span id="cb8-10" class="hljs-ln-code"><a href="#cb8-10"></a>}</span>
<span id="cb8-11" class="hljs-ln-code"><a href="#cb8-11"></a></span>
<span id="cb8-12" class="hljs-ln-code"><a href="#cb8-12"></a><span class="co"># here's the loop</span></span>
<span id="cb8-13" class="hljs-ln-code"><a href="#cb8-13"></a>result <span class="ot">&lt;-</span> purrr<span class="sc">::</span><span class="fu">map</span>(food, heat_the_food)</span>
<span id="cb8-14" class="hljs-ln-code"><a href="#cb8-14"></a></span>
<span id="cb8-15" class="hljs-ln-code"><a href="#cb8-15"></a>result</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>The code is easier to read, and we can use function names not comments to decribe what it’s doing</p>
</section>
<section id="in-detail" class="slide level2">
<h2>In detail</h2>
<p><code>purrr::map(food, heat_the_food)</code></p>
<p><code>purrr::map(.x, .f)</code></p>
<p>.x A list or atomic vector</p>
<p>.f A function</p>
<p><code>purrr::map(your_raw_material, function_containing_your_instructions)</code></p>
</section>
<section id="further-reading-training" class="slide level2">
<h2>Further reading / training</h2>
<p>Tom Jemmett’s excellent video on purrr:<br>
<a href="https://youtu.be/GxvccD8K49M" class="uri">https://youtu.be/GxvccD8K49M</a></p>
<p>The iteration chapter of R for Data Science:<br>
<a href="https://r4ds.had.co.nz/iteration.html#iteration" class="uri">https://r4ds.had.co.nz/iteration.html#iteration</a></p>
<p>The purrr docs:<br>
<a href="https://purrr.tidyverse.org" class="uri">https://purrr.tidyverse.org</a></p>
</section>
<section id="open-source" class="slide level2">
<h2>Open source</h2>
<p>This presentation:<br>
<a href="https://github.com/ThomUK/2023-NHSR-Conf-Presentation" class="uri">https://github.com/ThomUK/2023-NHSR-Conf-Presentation</a></p>
<div class="footer footer-default">

</div>
Expand Down
Loading

0 comments on commit f1734d1

Please sign in to comment.