Skip to content

Commit a27168f

Browse files
committed
Update the book
1 parent 6ee3f09 commit a27168f

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

docs/dataio.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ <h4><span class="header-section-number">D.3.1.5</span> stringsAsFactors</h4>
441441
</div>
442442
<div id="the-read-family" class="section level3">
443443
<h3><span class="header-section-number">D.3.2</span> The read Family</h3>
444-
<p>R also comes with some prepackaged short cuts for <code>read.table</code>, shown in <a href="dataio.html#tab:shortcuts">D.1</a>.</p>
444+
<p>R also comes with some prepackaged short cuts for <code>read.table</code>, shown in Table <a href="dataio.html#tab:shortcuts">D.1</a>.</p>
445445
<table>
446446
<caption><span id="tab:shortcuts">Table D.1: </span> R’s read functions. You can overwrite any of the default arguments as necessary.</caption>
447447
<colgroup>
@@ -522,7 +522,7 @@ <h3><span class="header-section-number">D.3.4</span> HTML Links</h3>
522522
</div>
523523
<div id="saving-plain-text-files" class="section level3">
524524
<h3><span class="header-section-number">D.3.5</span> Saving Plain-Text Files</h3>
525-
<p>Once your data is in R, you can save it to any file format that R supports. If you’d like to save it as a plain-text file, you can use the +write+ family of functions. The three basic write functions appear in <a href="dataio.html#tab:write">D.2</a>. Use <code>write.csv</code> to save your data as a <em>.csv</em> file and <code>write.table</code> to save your data as a tab delimited document or a document with more exotic separators.</p>
525+
<p>Once your data is in R, you can save it to any file format that R supports. If you’d like to save it as a plain-text file, you can use the +write+ family of functions. The three basic write functions appear in Table <a href="dataio.html#tab:write">D.2</a>. Use <code>write.csv</code> to save your data as a <em>.csv</em> file and <code>write.table</code> to save your data as a tab delimited document or a document with more exotic separators.</p>
526526
<table>
527527
<caption><span id="tab:write">Table D.2: </span> R saves data sets to plain-text files with the write family of functions</caption>
528528
<colgroup>
@@ -568,7 +568,7 @@ <h3><span class="header-section-number">D.3.5</span> Saving Plain-Text Files</h3
568568
<h3><span class="header-section-number">D.3.6</span> Compressing Files</h3>
569569
<p>To compress a plain-text file, surround the file name or file path with the function <code>bzfile</code>, <code>gzfile</code>, or <code>xzfile</code>. For example:</p>
570570
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">write.csv</span>(poker, <span class="dt">file =</span> <span class="kw">bzfile</span>(<span class="st">&quot;data/poker.csv.bz2&quot;</span>), <span class="dt">row.names =</span> <span class="ot">FALSE</span>)</code></pre>
571-
<p>Each of these functions will compress the output with a different type of compression format, shown in <a href="dataio.html#tab:compression">D.3</a>.</p>
571+
<p>Each of these functions will compress the output with a different type of compression format, shown in Table <a href="dataio.html#tab:compression">D.3</a>.</p>
572572
<table>
573573
<caption><span id="tab:compression">Table D.3: </span> R comes with three helper functions for compressing files</caption>
574574
<thead>
@@ -684,7 +684,7 @@ <h3><span class="header-section-number">D.5.5</span> Writing Spreadsheets</h3>
684684
<div id="loading-files-from-other-programs" class="section level2">
685685
<h2><span class="header-section-number">D.6</span> Loading Files from Other Programs</h2>
686686
<p>You should follow the same advice I gave you for Excel files whenever you wish to work with file formats native to other programs: open the file in the original program and export the data as a plain-text file, usually a CSV. This will ensure the most faithful transcription of the data in the file, and it will usually give you the most options for customizing how the data is transcribed.</p>
687-
<p>Sometimes, however, you may acquire a file but not the program it came from. As a result, you won’t be able to open the file in its native program and export it as a text file. In this case, you can use one of the functions in <a href="dataio.html#tab:others">D.4</a> to open the file. These functions mostly come in R’s <code>foreign</code> package. Each attempts to read in a different file format with as few hiccups as possible.</p>
687+
<p>Sometimes, however, you may acquire a file but not the program it came from. As a result, you won’t be able to open the file in its native program and export it as a text file. In this case, you can use one of the functions in Table <a href="dataio.html#tab:others">D.4</a> to open the file. These functions mostly come in R’s <code>foreign</code> package. Each attempts to read in a different file format with as few hiccups as possible.</p>
688688
<table>
689689
<caption><span id="tab:others">Table D.4: </span> A number of functions will attempt to read the file types of other data-analysis programs</caption>
690690
<thead>

docs/debug.html

+6-6
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,14 @@ <h2><span class="header-section-number">E.1</span> traceback</h2>
379379
## 4993: fifth() at #1
380380
## ...</code></pre>
381381
<p>Notice that there are 5,000 lines of output in this <code>traceback</code>. If you are using RStudio, you will not get to see the traceback of an infinite recursion error (I used the Mac GUI to get this output). RStudio represses the traceback for infinite recursion errors to prevent the large call stacks from pushing your console history out of R’s memory buffer. With RStudio, you will have to recognize the infinite recursion error by its error message. However, you can still see the imposing <code>traceback</code> by running things in a UNIX shell or the Windows or Mac GUIs.</p>
382-
<p>RStudio makes it very easy to use <code>traceback</code>. You do not even need to type in the function name. Whenever an error occurs, RStudio will display it in a gray box with two options. The first is Show Traceback, shown in <a href="debug.html#fig:show-traceback">E.1</a>.</p>
382+
<p>RStudio makes it very easy to use <code>traceback</code>. You do not even need to type in the function name. Whenever an error occurs, RStudio will display it in a gray box with two options. The first is Show Traceback, shown in Figure <a href="debug.html#fig:show-traceback">E.1</a>.</p>
383383
<div class="figure"><span id="fig:show-traceback"></span>
384384
<img src="images/hopr_ae01.png" alt="RStudio's Show Traceback option." />
385385
<p class="caption">
386386
Figure E.1: RStudio’s Show Traceback option.
387387
</p>
388388
</div>
389-
<p>If you click Show Traceback, RStudio will expand the gray box and display the <code>traceback</code> call stack, as in <a href="debug.html#fig:hide-traceback">E.2</a>. The Show Traceback option will persist beside an error message in your console, even as you write new commands. This means that you can go back and look at the call stacks for all errors—not just the most recent error.</p>
389+
<p>If you click Show Traceback, RStudio will expand the gray box and display the <code>traceback</code> call stack, as in Figure <a href="debug.html#fig:hide-traceback">E.2</a>. The Show Traceback option will persist beside an error message in your console, even as you write new commands. This means that you can go back and look at the call stacks for all errors—not just the most recent error.</p>
390390
<p>Imagine that you’ve used <code>traceback</code> to pinpoint a function that you think might cause a bug. Now what should you do? You should try to figure out what the function did to cause an error while it ran (if it did anything). You can examine how the function runs with <code>browser</code>.</p>
391391
<div class="figure"><span id="fig:hide-traceback"></span>
392392
<img src="images/hopr_ae02.png" alt="RStudio's Traceback display." />
@@ -433,7 +433,7 @@ <h2><span class="header-section-number">E.2</span> browser</h2>
433433
symbols &lt;-<span class="st"> </span><span class="kw">get_symbols</span>()
434434
<span class="kw">structure</span>(<span class="kw">score</span>(symbols), <span class="dt">symbols =</span> symbols, <span class="dt">class =</span> <span class="st">&quot;slots&quot;</span>)
435435
}</code></pre>
436-
<p>When you run <code>play</code>, <code>play</code> will call <code>get_symbols</code> and then <code>score</code>. As R works through <code>score</code>, it will come across the call to <code>browser</code> and run it. When R runs this call, several things will happen, as in <a href="debug.html#fig:browser">E.3</a>. First, R will stop running <code>score</code>. Second, the command prompt will change to <code>browser[1]&gt;</code> and R will give me back control; I can now type new commands in at the new command prompt. Third, three buttons will appear above the console pane: Next, Continue, and Stop. Fourth, RStudio will display the source code for <code>score</code> in the scripts pane, and it will highlight the line that contains <code>browser()</code>. Fifth, the environments tab will change. Instead of revealing the objects that are saved in the global environment, it will reveal the objects that are saved in the runtime environment of <code>score</code> (see <a href="environments.html#environments-1">Environments</a> for an explanation of R’s environment system). Sixth, RStudio will open a new Traceback pane, which shows the call stack RStudio took to get to <code>browser</code>. The most recent function, <code>score</code>, will be highlighted.</p>
436+
<p>When you run <code>play</code>, <code>play</code> will call <code>get_symbols</code> and then <code>score</code>. As R works through <code>score</code>, it will come across the call to <code>browser</code> and run it. When R runs this call, several things will happen, as in Figure <a href="debug.html#fig:browser">E.3</a>. First, R will stop running <code>score</code>. Second, the command prompt will change to <code>browser[1]&gt;</code> and R will give me back control; I can now type new commands in at the new command prompt. Third, three buttons will appear above the console pane: Next, Continue, and Stop. Fourth, RStudio will display the source code for <code>score</code> in the scripts pane, and it will highlight the line that contains <code>browser()</code>. Fifth, the environments tab will change. Instead of revealing the objects that are saved in the global environment, it will reveal the objects that are saved in the runtime environment of <code>score</code> (see <a href="environments.html#environments-1">Environments</a> for an explanation of R’s environment system). Sixth, RStudio will open a new Traceback pane, which shows the call stack RStudio took to get to <code>browser</code>. The most recent function, <code>score</code>, will be highlighted.</p>
437437
<p>I’m now in a new R mode, called <em>browser mode</em>. Browser mode is designed to help you uncover bugs, and the new display in RStudio is designed to help you navigate this mode.</p>
438438
<p>Any command that you run in browser mode will be evaluated in the context of the runtime environment of the function that called <code>browser</code>. This will be the function that is highlighted in the new Traceback pane. Here, that function is <code>score</code>. So while we are in browser mode, the active environment will be <code>score</code>’s runtime environment. This lets you do two things.</p>
439439
<div class="figure"><span id="fig:browser"></span>
@@ -448,7 +448,7 @@ <h2><span class="header-section-number">E.2</span> browser</h2>
448448

449449
Browse[<span class="dv">1</span>]<span class="op">&gt;</span><span class="st"> </span>same
450450
## [1] FALSE</code></pre>
451-
<p>Second, you can run code and see the same results that <code>score</code> would see. For example, you could run the remaining lines of the <code>score</code> function and see if they do anything unusual. You could run these lines by typing them into the command prompt, or you could use the three navigation buttons that now appear above the prompt, as in <a href="debug.html#fig:browser-buttons">E.4</a>.</p>
451+
<p>Second, you can run code and see the same results that <code>score</code> would see. For example, you could run the remaining lines of the <code>score</code> function and see if they do anything unusual. You could run these lines by typing them into the command prompt, or you could use the three navigation buttons that now appear above the prompt, as in Figure <a href="debug.html#fig:browser-buttons">E.4</a>.</p>
452452
<p>The first button, Next, will run the next line of code in <code>score</code>. The highlighted line in the scripts pane will advance by one line to show you your new location in the <code>score</code> function. If the next line begins a code chunk, like a +for+ loop or an <code>if</code> tree, R will run the whole chunk and will highlight the whole chunk in the script window.</p>
453453
<p>The second button, Continue, will run all of the remaining lines of <code>score</code> and then exit the browser mode.</p>
454454
<p>The third button, Stop, will exit browser mode without running any more lines of <code>score</code>.</p>
@@ -464,7 +464,7 @@ <h2><span class="header-section-number">E.2</span> browser</h2>
464464
</div>
465465
<div id="break-points" class="section level2">
466466
<h2><span class="header-section-number">E.3</span> Break Points</h2>
467-
<p>RStudio’s break points provide a graphical way to add a <code>browser</code> statement to a function. To use them, open the script where you’ve defined a function. Then click to the left of the line number of the line of code in the function body where you’d like to add the browser statement. A hollow red dot will appear to show you where the break point will occur. Then run the script by clicking the Source button at the top of the Scripts pane. The hollow dot will turn into a solid red dot to show that the function has a break point (see <a href="debug.html#fig:break-point">E.5</a>).</p>
467+
<p>RStudio’s break points provide a graphical way to add a <code>browser</code> statement to a function. To use them, open the script where you’ve defined a function. Then click to the left of the line number of the line of code in the function body where you’d like to add the browser statement. A hollow red dot will appear to show you where the break point will occur. Then run the script by clicking the Source button at the top of the Scripts pane. The hollow dot will turn into a solid red dot to show that the function has a break point (see Figure <a href="debug.html#fig:break-point">E.5</a>).</p>
468468
<p>R will treat the break point like a <code>browser</code> statement, going into browser mode when it encounters it. You can remove a break point by clicking on the red dot. The dot will disappear, and the break point will be removed.</p>
469469
<div class="figure"><span id="fig:break-point"></span>
470470
<img src="images/hopr_ae05.png" alt="Break points provide the graphical equivalent of a browser statement." />
@@ -484,7 +484,7 @@ <h2><span class="header-section-number">E.4</span> debug</h2>
484484
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">isdebugged</span>(sample)
485485
## FALSE</code></pre>
486486
<p>If this is all too much of a hassle, you can do what I do and use <code>debugonce</code> instead of <code>debug</code>. R will enter browser mode the very next time it runs the function but will automatically undebug the function afterward. If you need to browse through the function again, you can just run <code>debugonce</code> on it a second time.</p>
487-
<p>You can recreate <code>debugonce</code> in RStudio whenever an error occurs. “Rerun with debug” will appear in the grey error box beneath Show Traceback (<a href="debug.html#fig:show-traceback">E.1</a>). If you click this option, RStudio will rerun the command as if you had first run <code>debugonce</code> on it. R will immediately go into browser mode, allowing you to step through the code. The browser behavior will only occur on this run of the code. You do not need to worry about calling <code>undebug</code> when you are done.</p>
487+
<p>You can recreate <code>debugonce</code> in RStudio whenever an error occurs. “Rerun with debug” will appear in the grey error box beneath Show Traceback (Figure <a href="debug.html#fig:show-traceback">E.1</a>). If you click this option, RStudio will rerun the command as if you had first run <code>debugonce</code> on it. R will immediately go into browser mode, allowing you to step through the code. The browser behavior will only occur on this run of the code. You do not need to worry about calling <code>undebug</code> when you are done.</p>
488488
</div>
489489
<div id="trace" class="section level2">
490490
<h2><span class="header-section-number">E.5</span> trace</h2>

0 commit comments

Comments
 (0)