Re-render book for O'Reilly

This commit is contained in:
Hadley Wickham
2023-01-12 17:22:57 -06:00
parent 28671ed8bd
commit 360d65ae47
113 changed files with 4957 additions and 2997 deletions

View File

@@ -1,9 +1,9 @@
<section data-type="chapter" id="chp-workflow-scripts">
<h1><span id="sec-workflow-scripts-projects" class="quarto-section-identifier d-none d-lg-block"><span class="chapter-title">Workflow: scripts and projects</span></span></h1><p>This chapter will introduce you to two very important tools for organizing your code: scripts and projects.</p>
<h1><span id="sec-workflow-scripts-projects" class="quarto-section-identifier d-none d-lg-block"><span class="chapter-title">Workflow: scripts and projects</span></span></h1><p>This chapter will introduce you to two essential tools for organizing your code: scripts and projects.</p>
<section id="scripts" data-type="sect1">
<h1>
Scripts</h1>
<p>So far, you have used the console to run code. Thats a great place to start, but youll find it gets cramped pretty quickly as you create more complex ggplot2 graphics and longer dplyr pipelines. To give yourself more room to work, use the script editor. Open it up by clicking the File menu, and selecting New File, then R script, or using the keyboard shortcut Cmd/Ctrl + Shift + N. Now youll see four panes, as in <a href="#fig-rstudio-script" data-type="xref">#fig-rstudio-script</a>. The script editor is a great place to put code you care about. Keep experimenting in the console, but once you have written code that works and does what you want, put it in the script editor.</p>
<p>So far, you have used the console to run code. Thats a great place to start, but youll find it gets cramped pretty quickly as you create more complex ggplot2 graphics and longer dplyr pipelines. To give yourself more room to work, use the script editor. Open it up by clicking the File menu, selecting New File, then R script, or using the keyboard shortcut Cmd/Ctrl + Shift + N. Now youll see four panes, as in <a href="#fig-rstudio-script" data-type="xref">#fig-rstudio-script</a>. The script editor is a great place to put code you care about. Keep experimenting in the console, but once you have written code that works and does what you want, put it in the script editor.</p>
<div class="cell">
<div class="cell-output-display">
@@ -16,7 +16,7 @@ Scripts</h1>
<section id="running-code" data-type="sect2">
<h2>
Running code</h2>
<p>The script editor is a great place to build up complex ggplot2 plots or long sequences of dplyr manipulations. The key to using the script editor effectively is to memorize one of the most important keyboard shortcuts: Cmd/Ctrl + Enter. This executes the current R expression in the console. For example, take the code below. If your cursor is at █, pressing Cmd/Ctrl + Enter will run the complete command that generates <code>not_cancelled</code>. It will also move the cursor to the next statement (beginning with <code>not_cancelled |&gt;</code>). That makes it easy to step through your complete script by repeatedly pressing Cmd/Ctrl + Enter.</p>
<p>The script editor is an excellent place for building complex ggplot2 plots or long sequences of dplyr manipulations. The key to using the script editor effectively is to memorize one of the most important keyboard shortcuts: Cmd/Ctrl + Enter. This executes the current R expression in the console. For example, take the code below. If your cursor is at █, pressing Cmd/Ctrl + Enter will run the complete command that generates <code>not_cancelled</code>. It will also move the cursor to the following statement (beginning with <code>not_cancelled |&gt;</code>). That makes it easy to step through your complete script by repeatedly pressing Cmd/Ctrl + Enter.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">library(dplyr)
library(nycflights13)
@@ -29,14 +29,14 @@ not_cancelled |&gt;
summarize(mean = mean(dep_delay))</pre>
</div>
<p>Instead of running your code expression-by-expression, you can also execute the complete script in one step with Cmd/Ctrl + Shift + S. Doing this regularly is a great way to ensure that youve captured all the important parts of your code in the script.</p>
<p>We recommend that you always start your script with the packages that you need. That way, if you share your code with others, they can easily see which packages they need to install. Note, however, that you should never include <code><a href="https://rdrr.io/r/utils/install.packages.html">install.packages()</a></code> in a script that you share. Its very antisocial to change settings on someone elses computer!</p>
<p>We recommend you always start your script with the packages you need. That way, if you share your code with others, they can easily see which packages they need to install. Note, however, that you should never include <code><a href="https://rdrr.io/r/utils/install.packages.html">install.packages()</a></code> in a script you share. Its very antisocial to change settings on someone elses computer!</p>
<p>When working through future chapters, we highly recommend starting in the script editor and practicing your keyboard shortcuts. Over time, sending code to the console in this way will become so natural that you wont even think about it.</p>
</section>
<section id="rstudio-diagnostics" data-type="sect2">
<h2>
RStudio diagnostics</h2>
<p>In script editor, RStudio will highlight syntax errors with a red squiggly line and a cross in the sidebar:</p>
<p>In the script editor, RStudio will highlight syntax errors with a red squiggly line and a cross in the sidebar:</p>
<div class="cell">
<div class="cell-output-display">
<p><img src="screenshots/rstudio-diagnostic.png" alt="Script editor with the script x y &lt;- 10. A red X indicates that there is syntax error. The syntax error is also highlighted with a red squiggly line." width="148"/></p>
@@ -51,7 +51,7 @@ RStudio diagnostics</h2>
<p>RStudio will also let you know about potential problems:</p>
<div class="cell">
<div class="cell-output-display">
<p><img src="screenshots/rstudio-diagnostic-warn.png" alt="Script editor with the script 3 == NA. A yellow exclamation park indicates that there may be a potential problem. Hovering over the exclamation mark shows a text box with the text use is.na to check whether expression evaluates to NA." width="439"/></p>
<p><img src="screenshots/rstudio-diagnostic-warn.png" alt="Script editor with the script 3 == NA. A yellow exclamation mark indicates that there may be a potential problem. Hovering over the exclamation mark shows a text box with the text use is.na to check whether expression evaluates to NA." width="439"/></p>
</div>
</div>
</section>
@@ -134,7 +134,7 @@ Where does your analysis live?</h2>
<pre data-type="programlisting" data-code-language="r">getwd()
#&gt; [1] "/Users/hadley/Documents/r4ds/r4ds"</pre>
</div>
<p>As a beginning R user, its OK to let your working direction be your home directory, documents directory, or any other weird directory on your computer. But youre nine chapters into this book, and youre no longer a rank beginner. Very soon now you should evolve to organizing your projects into directories and, when working on a project, set Rs working directory to the associated directory.</p>
<p>As a beginning R user, its OK to let your working directory be your home directory, documents directory, or any other weird directory on your computer. But youre nine chapters into this book, and youre no longer a rank beginner. Very soon now you should evolve to organizing your projects into directories and, when working on a project, set Rs working directory to the associated directory.</p>
<p>You can set the working directory from within R but <strong>we</strong> <strong>do not recommend it</strong>:</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">setwd("/path/to/my/CoolProject")</pre>
@@ -177,7 +177,7 @@ RStudio projects</h2>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">library(tidyverse)
ggplot(diamonds, aes(carat, price)) +
ggplot(diamonds, aes(x = carat, y = price)) +
geom_hex()
ggsave("diamonds.pdf")
@@ -210,14 +210,14 @@ Summary</h1>
<h1>
Exercises</h1>
<ol type="1"><li><p>Go to the RStudio Tips Twitter account, <a href="https://twitter.com/rstudiotips" class="uri">https://twitter.com/rstudiotips</a> and find one tip that looks interesting. Practice using it!</p></li>
<li><p>What other common mistakes will RStudio diagnostics report? Read <a href="https://support.rstudio.com/hc/en-us/articles/205753617-Code-Diagnostics" class="uri">https://support.rstudio.com/hc/en-us/articles/205753617-Code-Diagnostics</a> to find out.</p></li>
<li><p>What other common mistakes will RStudio diagnostics report? Read <a href="https://support.posit.co/hc/en-us/articles/205753617-Code-Diagnostics" class="uri">https://support.posit.co/hc/en-us/articles/205753617-Code-Diagnostics</a> to find out.</p></li>
</ol></section>
<section id="summary-1" data-type="sect1">
<h1>
Summary</h1>
<p>In this chapter, youve learned how to organize your R code in scripts (files) and projects (directories). Much like code style, this may feel like busywork at first. But as you accumulate more code across multiple projects, youll learn to appreciate how a little up front organisation can save you a bunch of time down the road.</p>
<p>Next up, well switch back to data science tooling to talk about exploratory data analysis (or EDA for short), a philosophy and set of tools that you can use with your data to start to get a sense of whats going on.</p>
<p>Next up, youll learn about how to get help and how to ask good coding questions.</p>
</section>