More minor page count tweaks & fixes
And re-convert with latest htmlbook
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<section data-type="chapter" id="chp-functions">
|
||||
<h1><span id="sec-functions" class="quarto-section-identifier d-none d-lg-block"><span class="chapter-title">Functions</span></span></h1>
|
||||
<section id="introduction" data-type="sect1">
|
||||
<section id="functions-introduction" data-type="sect1">
|
||||
<h1>
|
||||
Introduction</h1>
|
||||
<p>One of the best ways to improve your reach as a data scientist is to write functions. Functions allow you to automate common tasks in a more powerful and general way than copy-and-pasting. Writing a function has three big advantages over using copy-and-paste:</p>
|
||||
@@ -13,7 +13,7 @@ Introduction</h1>
|
||||
<li>Plot functions that take a data frame as input and return a plot as output.</li>
|
||||
</ul><p>Each of these sections include many examples to help you generalize the patterns that you see. These examples wouldn’t be possible without the help of folks of twitter, and we encourage follow the links in the comment to see original inspirations. You might also want to read the original motivating tweets for <a href="https://twitter.com/hadleywickham/status/1571603361350164486">general functions</a> and <a href="https://twitter.com/hadleywickham/status/1574373127349575680">plotting functions</a> to see even more functions.</p>
|
||||
|
||||
<section id="prerequisites" data-type="sect2">
|
||||
<section id="functions-prerequisites" data-type="sect2">
|
||||
<h2>
|
||||
Prerequisites</h2>
|
||||
<p>We’ll wrap up a variety of functions from around the tidyverse. We’ll also use nycflights13 as a source of familiar data to use our functions with.</p>
|
||||
@@ -273,13 +273,18 @@ mape <- function(actual, predicted) {
|
||||
</div>
|
||||
<div data-type="note"><h1>
|
||||
RStudio
|
||||
</h1><p>Once you start writing functions, there are two RStudio shortcuts that are super useful:</p><ul><li><p>To find the definition of a function that you’ve written, place the cursor on the name of the function and press <code>F2</code>.</p></li>
|
||||
</h1>
|
||||
|
||||
|
||||
<p>Once you start writing functions, there are two RStudio shortcuts that are super useful:</p>
|
||||
<ul><li><p>To find the definition of a function that you’ve written, place the cursor on the name of the function and press <code>F2</code>.</p></li>
|
||||
<li><p>To quickly jump to a function, press <code>Ctrl + .</code> to open the fuzzy file and function finder and type the first few letters of your function name. You can also navigate to files, Quarto sections, and more, making it a very handy navigation tool.</p></li>
|
||||
</ul></div>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
<section id="exercises" data-type="sect2">
|
||||
<section id="functions-exercises" data-type="sect2">
|
||||
<h2>
|
||||
Exercises</h2>
|
||||
<ol type="1"><li>
|
||||
@@ -610,7 +615,7 @@ diamonds |> count_wide(c(clarity, color), cut)
|
||||
<p>While our examples have mostly focused on dplyr, tidy evaluation also underpins tidyr, and if you look at the <code><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider()</a></code> docs you can see that <code>names_from</code> uses tidy-selection.</p>
|
||||
</section>
|
||||
|
||||
<section id="exercises-1" data-type="sect2">
|
||||
<section id="functions-exercises-1" data-type="sect2">
|
||||
<h2>
|
||||
Exercises</h2>
|
||||
<ol type="1"><li>
|
||||
@@ -691,9 +696,6 @@ diamonds |> histogram(carat, 0.1)</pre>
|
||||
<pre data-type="programlisting" data-code-language="r">diamonds |>
|
||||
histogram(carat, 0.1) +
|
||||
labs(x = "Size (in carats)", y = "Number of diamonds")</pre>
|
||||
<div class="cell-output-display">
|
||||
<p><img src="functions_files/figure-html/unnamed-chunk-47-1.png" class="img-fluid" width="576"/></p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<section id="more-variables" data-type="sect2">
|
||||
@@ -706,15 +708,13 @@ linearity_check <- function(df, x, y) {
|
||||
df |>
|
||||
ggplot(aes(x = {{ x }}, y = {{ y }})) +
|
||||
geom_point() +
|
||||
geom_smooth(method = "loess", color = "red", se = FALSE) +
|
||||
geom_smooth(method = "lm", color = "blue", se = FALSE)
|
||||
geom_smooth(method = "loess", formula = y ~ x, color = "red", se = FALSE) +
|
||||
geom_smooth(method = "lm", formula = y ~ x, color = "blue", se = FALSE)
|
||||
}
|
||||
|
||||
starwars |>
|
||||
filter(mass < 1000) |>
|
||||
linearity_check(mass, height)
|
||||
#> `geom_smooth()` using formula = 'y ~ x'
|
||||
#> `geom_smooth()` using formula = 'y ~ x'</pre>
|
||||
linearity_check(mass, height)</pre>
|
||||
<div class="cell-output-display">
|
||||
<p><img src="functions_files/figure-html/unnamed-chunk-48-1.png" class="img-fluid" width="576"/></p>
|
||||
</div>
|
||||
@@ -837,15 +837,6 @@ density <- function(color, facets, binwidth = 0.1) {
|
||||
density()
|
||||
density(cut)
|
||||
density(cut, clarity)</pre>
|
||||
<div class="cell-output-display">
|
||||
<p><img src="functions_files/figure-html/unnamed-chunk-54-1.png" class="img-fluid" width="576"/></p>
|
||||
</div>
|
||||
<div class="cell-output-display">
|
||||
<p><img src="functions_files/figure-html/unnamed-chunk-54-2.png" class="img-fluid" width="576"/></p>
|
||||
</div>
|
||||
<div class="cell-output-display">
|
||||
<p><img src="functions_files/figure-html/unnamed-chunk-54-3.png" class="img-fluid" width="576"/></p>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@@ -880,7 +871,7 @@ diamonds |> histogram(carat, 0.1)</pre>
|
||||
<p>You can use the same approach in any other place where you want to supply a string in a ggplot2 plot.</p>
|
||||
</section>
|
||||
|
||||
<section id="exercises-2" data-type="sect2">
|
||||
<section id="functions-exercises-2" data-type="sect2">
|
||||
<h2>
|
||||
Exercises</h2>
|
||||
<p>Build up a rich plotting function by incrementally implementing each of the steps below:</p>
|
||||
@@ -926,7 +917,7 @@ density <- function(color, facets, binwidth = 0.1) {
|
||||
</div>
|
||||
<p>As you can see we recommend putting extra spaces inside of <code>{{ }}</code>. This makes it very obvious that something unusual is happening.</p>
|
||||
|
||||
<section id="exercises-3" data-type="sect2">
|
||||
<section id="functions-exercises-3" data-type="sect2">
|
||||
<h2>
|
||||
Exercises</h2>
|
||||
<ol type="1"><li>
|
||||
@@ -946,7 +937,7 @@ f3 <- function(x, y) {
|
||||
</ol></section>
|
||||
</section>
|
||||
|
||||
<section id="summary" data-type="sect1">
|
||||
<section id="functions-summary" data-type="sect1">
|
||||
<h1>
|
||||
Summary</h1>
|
||||
<p>In this chapter, you learned how to write functions for three useful scenarios: creating a vector, creating a data frames, or creating a plot. Along the way you saw many examples, which hopefully started to get your creative juices flowing, and gave you some ideas for where functions might help your analysis code.</p>
|
||||
|
||||
Reference in New Issue
Block a user