Fix code language
This commit is contained in:
		@@ -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><-</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 <- flights |> filter(air_time < 60)
 | 
			
		||||
 | 
			
		||||
# Avoid:
 | 
			
		||||
@@ -30,7 +30,7 @@ SHORTFLIGHTS <- flights |> filter(air_time < 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><</code>, …), and around the assignment operator (<code><-</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 <- (a + b)^2 / d
 | 
			
		||||
 | 
			
		||||
# Avoid
 | 
			
		||||
@@ -38,7 +38,7 @@ z<-( a + b ) ^ 2/d</pre>
 | 
			
		||||
</div>
 | 
			
		||||
<p>Don’t 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>It’s OK to add extra spaces if it improves alignment. For example, if you’re 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 |> 
 | 
			
		||||
<pre data-type="programlisting" data-code-language="r">flights |> 
 | 
			
		||||
  mutate(
 | 
			
		||||
    speed      = air_time / distance,
 | 
			
		||||
    dep_hour   = dep_time %/% 100,
 | 
			
		||||
@@ -60,7 +60,7 @@ mean (x ,na.rm=TRUE)</pre>
 | 
			
		||||
Pipes</h1>
 | 
			
		||||
<p><code>|></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 |>  
 | 
			
		||||
  filter(!is.na(arr_delay), !is.na(tailnum)) |> 
 | 
			
		||||
  count(dest)
 | 
			
		||||
@@ -70,7 +70,7 @@ flights|>filter(!is.na(arr_delay), !is.na(tailnum))|>count(dest)</pre>
 | 
			
		||||
</div>
 | 
			
		||||
<p>If the function you’re 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 doesn’t 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 doesn’t 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 |>  
 | 
			
		||||
  group_by(tailnum) |> 
 | 
			
		||||
  summarize(
 | 
			
		||||
@@ -87,7 +87,7 @@ flights |>
 | 
			
		||||
</div>
 | 
			
		||||
<p>After the first step of the pipeline, indent each line by two spaces. If you’re 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 |>  
 | 
			
		||||
  group_by(tailnum) |> 
 | 
			
		||||
  summarize(
 | 
			
		||||
@@ -112,7 +112,7 @@ flights|>
 | 
			
		||||
</div>
 | 
			
		||||
<p>It’s OK to shirk some of these rules if your pipeline fits easily on one line. But in our collective experience, it’s common for short snippets to grow longer, so you’ll 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 |> mutate(y = x + 1)
 | 
			
		||||
 | 
			
		||||
# While this takes up 4x as many lines, it's easily extended to 
 | 
			
		||||
@@ -130,7 +130,7 @@ df |>
 | 
			
		||||
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>|></code>.</p>
 | 
			
		||||
<div class="cell">
 | 
			
		||||
<pre data-type="programlisting" data-code-language="downlit">flights |> 
 | 
			
		||||
<pre data-type="programlisting" data-code-language="r">flights |> 
 | 
			
		||||
  group_by(month) |> 
 | 
			
		||||
  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 |> 
 | 
			
		||||
<pre data-type="programlisting" data-code-language="r">flights |> 
 | 
			
		||||
  group_by(dest) |> 
 | 
			
		||||
  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|>filter(dest=="IAH")|>group_by(year,month,day)|>summarize(n=n(),delay=mean(arr_delay,na.rm=TRUE))|>filter(n>10)
 | 
			
		||||
<pre data-type="programlisting" data-code-language="r">flights|>filter(dest=="IAH")|>group_by(year,month,day)|>summarize(n=n(),delay=mean(arr_delay,na.rm=TRUE))|>filter(n>10)
 | 
			
		||||
 | 
			
		||||
flights|>filter(carrier=="UA",dest%in%c("IAH","HOU"),sched_dep_time>0900,sched_arr_time<2000)|>group_by(flight)|>summarize(delay=mean(arr_delay,na.rm=TRUE),cancelled=sum(is.na(arr_delay)),n=n())|>filter(n>10)</pre>
 | 
			
		||||
</div>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user