Fix code language

This commit is contained in:
Hadley Wickham
2022-11-18 11:26:25 -06:00
parent 69b4597f3b
commit 868a35ca71
29 changed files with 912 additions and 907 deletions

View File

@@ -7,7 +7,7 @@
</figure>
</div>
</div><div class="cell">
<pre data-type="programlisting" data-code-language="downlit">library(tidyverse)
<pre data-type="programlisting" data-code-language="r">library(tidyverse)
library(nycflights13)</pre>
</div>
<section id="names" data-type="sect1">
@@ -15,7 +15,7 @@ library(nycflights13)</pre>
Names</h1>
<p>We talked briefly about names in <a href="#sec-whats-in-a-name" data-type="xref">#sec-whats-in-a-name</a>. Remember that variable names (those created by <code>&lt;-</code> and those created by <code><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate()</a></code>) should use only lowercase letters, numbers, and <code>_</code>. Use <code>_</code> to separate words within a name.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit"># Strive for:
<pre data-type="programlisting" data-code-language="r"># Strive for:
short_flights &lt;- flights |&gt; filter(air_time &lt; 60)
# Avoid:
@@ -30,7 +30,7 @@ SHORTFLIGHTS &lt;- flights |&gt; filter(air_time &lt; 60)</pre>
Spaces</h1>
<p>Put spaces on either side of mathematical operators apart from <code>^</code> (i.e., <code>+</code>, <code>-</code>, <code>==</code>, <code>&lt;</code>, …), and around the assignment operator (<code>&lt;-</code>).</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit"># Strive for
<pre data-type="programlisting" data-code-language="r"># Strive for
z &lt;- (a + b)^2 / d
# Avoid
@@ -38,7 +38,7 @@ z&lt;-( a + b ) ^ 2/d</pre>
</div>
<p>Dont put spaces inside or outside parentheses for regular function calls. Always put a space after a comma, just like in regular English.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit"># Strive for
<pre data-type="programlisting" data-code-language="r"># Strive for
mean(x, na.rm = TRUE)
# Avoid
@@ -46,7 +46,7 @@ mean (x ,na.rm=TRUE)</pre>
</div>
<p>Its OK to add extra spaces if it improves alignment. For example, if youre creating multiple variables in <code><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate()</a></code>, you might want to add spaces so that all the <code>=</code> line up. This makes it easier to skim the code.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit">flights |&gt;
<pre data-type="programlisting" data-code-language="r">flights |&gt;
mutate(
speed = air_time / distance,
dep_hour = dep_time %/% 100,
@@ -60,7 +60,7 @@ mean (x ,na.rm=TRUE)</pre>
Pipes</h1>
<p><code>|&gt;</code> should always have a space before it and should typically be the last thing on a line. This makes it easier to add new steps, rearrange existing steps, modify elements within a step, and to get a 50,000 ft view by skimming the verbs on the left-hand side.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit"># Strive for
<pre data-type="programlisting" data-code-language="r"># Strive for
flights |&gt;
filter(!is.na(arr_delay), !is.na(tailnum)) |&gt;
count(dest)
@@ -70,7 +70,7 @@ flights|&gt;filter(!is.na(arr_delay), !is.na(tailnum))|&gt;count(dest)</pre>
</div>
<p>If the function youre piping into has named arguments (like <code><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate()</a></code> or <code><a href="https://dplyr.tidyverse.org/reference/summarise.html">summarize()</a></code>), put each argument on a new line. If the function doesnt have named arguments (like <code><a href="https://dplyr.tidyverse.org/reference/select.html">select()</a></code> or <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code>) keep everything on one line unless it doesnt fit, in which case you should put each argument on its own line.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit"># Strive for
<pre data-type="programlisting" data-code-language="r"># Strive for
flights |&gt;
group_by(tailnum) |&gt;
summarize(
@@ -87,7 +87,7 @@ flights |&gt;
</div>
<p>After the first step of the pipeline, indent each line by two spaces. If youre putting each argument on its own line, indent by an extra two spaces. Make sure <code>)</code> is on its own line, and un-indented to match the horizontal position of the function name.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit"># Strive for
<pre data-type="programlisting" data-code-language="r"># Strive for
flights |&gt;
group_by(tailnum) |&gt;
summarize(
@@ -112,7 +112,7 @@ flights|&gt;
</div>
<p>Its OK to shirk some of these rules if your pipeline fits easily on one line. But in our collective experience, its common for short snippets to grow longer, so youll usually save time in the long run by starting with all the vertical space you need.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit"># This fits compactly on one line
<pre data-type="programlisting" data-code-language="r"># This fits compactly on one line
df |&gt; mutate(y = x + 1)
# While this takes up 4x as many lines, it's easily extended to
@@ -130,7 +130,7 @@ df |&gt;
ggplot2</h1>
<p>The same basic rules that apply to the pipe also apply to ggplot2; just treat <code>+</code> the same way as <code>|&gt;</code>.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit">flights |&gt;
<pre data-type="programlisting" data-code-language="r">flights |&gt;
group_by(month) |&gt;
summarize(
delay = mean(arr_delay, na.rm = TRUE)
@@ -141,7 +141,7 @@ ggplot2</h1>
</div>
<p>Again, if you can fit all of the arguments to a function on to a single line, put each argument on its own line:</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit">flights |&gt;
<pre data-type="programlisting" data-code-language="r">flights |&gt;
group_by(dest) |&gt;
summarize(
distance = mean(distance),
@@ -164,7 +164,7 @@ ggplot2</h1>
Sectioning comments</h1>
<p>As your scripts get longer, you can use <strong>sectioning</strong> comments to break up your file into manageable pieces:</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit"># Load data --------------------------------------
<pre data-type="programlisting" data-code-language="r"># Load data --------------------------------------
# Plot data --------------------------------------</pre>
</div>
@@ -185,7 +185,7 @@ Exercises</h1>
<ol type="1"><li>
<p>Restyle the following pipelines following the guidelines above.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit">flights|&gt;filter(dest=="IAH")|&gt;group_by(year,month,day)|&gt;summarize(n=n(),delay=mean(arr_delay,na.rm=TRUE))|&gt;filter(n&gt;10)
<pre data-type="programlisting" data-code-language="r">flights|&gt;filter(dest=="IAH")|&gt;group_by(year,month,day)|&gt;summarize(n=n(),delay=mean(arr_delay,na.rm=TRUE))|&gt;filter(n&gt;10)
flights|&gt;filter(carrier=="UA",dest%in%c("IAH","HOU"),sched_dep_time&gt;0900,sched_arr_time&lt;2000)|&gt;group_by(flight)|&gt;summarize(delay=mean(arr_delay,na.rm=TRUE),cancelled=sum(is.na(arr_delay)),n=n())|&gt;filter(n&gt;10)</pre>
</div>