r4ds/oreilly/workflow-help.html

74 lines
7.7 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<section data-type="chapter" id="chp-workflow-help">
<h1><span id="sec-workflow-getting-help" class="quarto-section-identifier d-none d-lg-block"><span class="chapter-title">Workflow: getting help</span></span></h1><p>This book is not an island; there is no single resource that will allow you to master R. As you begin to apply the techniques described in this book to your own data, you will soon find questions that we do not answer. This section describes a few tips on how to get help and to help you keep learning.</p>
<section id="google-is-your-friend" data-type="sect1">
<h1>
Google is your friend</h1>
<p>If you get stuck, start with Google. Typically adding “R” to a query is enough to restrict it to relevant results: if the search isnt useful, it often means that there arent any R-specific results available. Google is particularly useful for error messages. If you get an error message and you have no idea what it means, try googling it! Chances are that someone else has been confused by it in the past, and there will be help somewhere on the web. (If the error message isnt in English, run <code>Sys.setenv(LANGUAGE = "en")</code> and re-run the code; youre more likely to find help for English error messages.)</p>
<p>If Google doesnt help, try <a href="https://stackoverflow.com">Stack Overflow</a>. Start by spending a little time searching for an existing answer, including <code>[R]</code>, to restrict your search to questions and answers that use R.</p>
</section>
<section id="making-a-reprex" data-type="sect1">
<h1>
Making a reprex</h1>
<p>If your googling doesnt find anything useful, its a really good idea to prepare a <strong>reprex,</strong> short for minimal <strong>repr</strong>oducible <strong>ex</strong>ample. A good reprex makes it easier for other people to help you, and often youll figure out the problem yourself in the course of making it. There are two parts to creating a reprex:</p>
<ul><li><p>First, you need to make your code reproducible. This means that you need to capture everything, i.e., include any <code><a href="https://rdrr.io/r/base/library.html">library()</a></code> calls and create all necessary objects. The easiest way to make sure youve done this is using the reprex package.</p></li>
<li><p>Second, you need to make it minimal. Strip away everything that is not directly related to your problem. This usually involves creating a much smaller and simpler R object than the one youre facing in real life or even using built-in data.</p></li>
</ul><p>That sounds like a lot of work! And it can be, but it has a great payoff:</p>
<ul><li><p>80% of the time, creating an excellent reprex reveals the source of your problem. Its amazing how often the process of writing up a self-contained and minimal example allows you to answer your own question.</p></li>
<li><p>The other 20% of the time, you will have captured the essence of your problem in a way that is easy for others to play with. This substantially improves your chances of getting help!</p></li>
</ul><p>When creating a reprex by hand, its easy to accidentally miss something, meaning your code cant be run on someone elses computer. Avoid this problem by using the reprex package, which is installed as part of the tidyverse. Lets say you copy this code onto your clipboard (or, on RStudio Server or Cloud, select it):</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">y &lt;- 1:4
mean(y)</pre>
</div>
<p>Then call <code>reprex()</code>, where the default target venue is GitHub:</p>
<pre data-type="programlisting" data-code-language="r">reprex::reprex()</pre>
<p>A nicely rendered HTML preview will display in RStudios Viewer (if youre in RStudio) or your default browser otherwise. The relevant bit of GitHub-flavored Markdown is ready to be pasted from your clipboard (on RStudio Server or Cloud, you will need to copy this yourself):</p>
<pre><code>``` r
y &lt;- 1:4
mean(y)
#&gt; [1] 2.5
```</code></pre>
<p>Heres what that Markdown would look like rendered in a GitHub issue:</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">y &lt;- 1:4
mean(y)
#&gt; [1] 2.5</pre>
</div>
<p>Anyone else can copy, paste, and run this immediately.</p>
<p>There are three things you need to include to make your example reproducible: required packages, data, and code.</p>
<ol type="1"><li><p><strong>Packages</strong> should be loaded at the top of the script so its easy to see which ones the example needs. This is a good time to check that youre using the latest version of each package; you may have discovered a bug thats been fixed since you installed or last updated the package. For packages in the tidyverse, the easiest way to check is to run <code>tidyverse_update()</code>.</p></li>
<li>
<p>The easiest way to include <strong>data</strong> is to use <code><a href="https://rdrr.io/r/base/dput.html">dput()</a></code> to generate the R code needed to recreate it. For example, to recreate the <code>mtcars</code> dataset in R, perform the following steps:</p>
<ol type="1"><li>Run <code>dput(mtcars)</code> in R</li>
<li>Copy the output</li>
<li>In reprex, type <code>mtcars &lt;-</code>, then paste.</li>
</ol><p>Try and find the smallest subset of your data that still reveals the problem.</p>
</li>
<li>
<p>Spend a little bit of time ensuring that your <strong>code</strong> is easy for others to read:</p>
<ul><li><p>Make sure youve used spaces and your variable names are concise yet informative.</p></li>
<li><p>Use comments to indicate where your problem lies.</p></li>
<li><p>Do your best to remove everything that is not related to the problem.</p></li>
</ul><p>The shorter your code is, the easier it is to understand and the easier it is to fix.</p>
</li>
</ol><p>Finish by checking that you have actually made a reproducible example by starting a fresh R session and copying and pasting your script.</p>
</section>
<section id="investing-in-yourself" data-type="sect1">
<h1>
Investing in yourself</h1>
<p>You should also spend some time preparing yourself to solve problems before they occur. Investing a little time in learning R each day will pay off handsomely in the long run. One way is to follow what the tidyverse team is doing on the <a href="https://www.tidyverse.org/blog/">tidyverse blog</a>. To keep up with the R community more broadly, we recommend reading <a href="https://rweekly.org">R Weekly</a>: its a community effort to aggregate the most interesting news in the R community each week.</p>
<p>If youre an active Twitter user, you might also want to follow Hadley (<a href="https://twitter.com/hadleywickham">@hadleywickham</a>), Mine (<a href="https://twitter.com/minebocek">@minebocek</a>), Garrett (<a href="https://twitter.com/statgarrett">@statgarrett</a>), or follow <a href="https://twitter.com/rstudiotips">@rstudiotips</a> to keep up with new features in the IDE. If you want the full fire hose of new developments, you can also read the (<a href="https://twitter.com/search?q=%23rstats"><code>#rstats</code></a>) hashtag. This is one of the key tools that Hadley and Mine use to keep up with new developments in the community.</p>
</section>
<section id="summary" data-type="sect1">
<h1>
Summary</h1>
<p>This chapter concludes the Whole Game part of the book. Youve now seen the most important parts of the data science process: visualization, transformation, tidying and importing. Now youve got a holistic view of the whole process, and we start to get into the details of small pieces.</p>
<p>The next part of the book, Visualize, does a deeper dive into the grammar of graphics and creating data visualizations with ggplot2, showcases how to use the tools youve learned so far to conduct exploratory data analysis, and introduces good practices for creating plots for communication.</p>
</section>
</section>