Skip to content

Commit

Permalink
Adds Figure and Table labels to the appendix
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettgman committed Oct 18, 2018
1 parent a165808 commit 0c6224c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion a1-starting.rmd
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ You can [download RStudio](http://www.rstudio.com/ide) for free. Just click the
Windows and Mac users usually do not program from a terminal window, so the Windows and Mac downloads for R come with a simple program that opens a terminal-like window for you to run R code in. This is what opens when you click the R icon on your Windows or Mac computer. These programs do a little more than the basic terminal window, but not much. You may hear people refer to them as the Windows or Mac R GUIs.
```

When you open RStudio, a window appears with three panes in it, as in \@ref(fig:layout). The largest pane is a console window. This is where you'll run your R code and see results. The console window is exactly what you'd see if you ran R from a UNIX console or the Windows or Mac GUIs. Everything else you see is unique to RStudio. Hidden in the other panes are a text editor, a graphics window, a debugger, a file manager, and much more. You'll learn about these panes as they become useful throughout the course of this book.
When you open RStudio, a window appears with three panes in it, as in Figure \@ref(fig:layout). The largest pane is a console window. This is where you'll run your R code and see results. The console window is exactly what you'd see if you ran R from a UNIX console or the Windows or Mac GUIs. Everything else you see is unique to RStudio. Hidden in the other panes are a text editor, a graphics window, a debugger, a file manager, and much more. You'll learn about these panes as they become useful throughout the course of this book.

```{r layout, echo = FALSE, fig.cap = "The RStudio IDE for R."}
knitr::include_graphics("images/hopr_aa01.png")
Expand Down
8 changes: 4 additions & 4 deletions a4-data.rmd
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ options(stringsAsFactors = TRUE)

### The read Family

R also comes with some prepackaged short cuts for `read.table`, shown in \@ref(tab:shortcuts).
R also comes with some prepackaged short cuts for `read.table`, shown in Table \@ref(tab:shortcuts).

Table: (\#tab:shortcuts) R's read functions. You can overwrite any of the default arguments as necessary.

Expand Down Expand Up @@ -250,7 +250,7 @@ Note that websites that begin with _https://_ are secure websites, which means R

### Saving Plain-Text Files

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 \@ref(tab:write). Use `write.csv` to save your data as a _.csv_ file and `write.table` to save your data as a tab delimited document or a document with more exotic separators.
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 \@ref(tab:write). Use `write.csv` to save your data as a _.csv_ file and `write.table` to save your data as a tab delimited document or a document with more exotic separators.

Table: (\#tab:write) R saves data sets to plain-text files with the write family of functions

Expand Down Expand Up @@ -292,7 +292,7 @@ To compress a plain-text file, surround the file name or file path with the func
write.csv(poker, file = bzfile("data/poker.csv.bz2"), row.names = FALSE)
```

Each of these functions will compress the output with a different type of compression format, shown in \@ref(tab:compression).
Each of these functions will compress the output with a different type of compression format, shown in Table \@ref(tab:compression).

Table: (\#tab:compression) R comes with three helper functions for compressing files

Expand Down Expand Up @@ -475,7 +475,7 @@ vignette("XLConnect")

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.

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 \@ref(tab:others) to open the file. These functions mostly come in R's `foreign` package. Each attempts to read in a different file format with as few hiccups as possible.
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 \@ref(tab:others) to open the file. These functions mostly come in R's `foreign` package. Each attempts to read in a different file format with as few hiccups as possible.

Table: (\#tab:others) A number of functions will attempt to read the file types of other data-analysis programs

Expand Down
12 changes: 6 additions & 6 deletions a5-debug.rmd
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ traceback()

Notice that there are 5,000 lines of output in this `traceback`. 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 `traceback` by running things in a UNIX shell or the Windows or Mac GUIs.

RStudio makes it very easy to use `traceback`. 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 \@ref(fig:show-traceback).
RStudio makes it very easy to use `traceback`. 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 \@ref(fig:show-traceback).

```{r show-traceback, echo = FALSE, fig.cap = "RStudio's Show Traceback option."}
knitr::include_graphics("images/hopr_ae01.png")
```

If you click Show Traceback, RStudio will expand the gray box and display the `traceback` call stack, as in \@ref(fig:hide-traceback). 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.
If you click Show Traceback, RStudio will expand the gray box and display the `traceback` call stack, as in Figure \@ref(fig:hide-traceback). 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.

Imagine that you've used `traceback` 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 `browser`.

Expand Down Expand Up @@ -135,7 +135,7 @@ play <- function() {
}
```

When you run `play`, `play` will call `get_symbols` and then `score`. As R works through `score`, it will come across the call to `browser` and run it. When R runs this call, several things will happen, as in \@ref(fig:browser). First, R will stop running `score`. Second, the command prompt will change to `browser[1]>` 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 `score` in the scripts pane, and it will highlight the line that contains `browser()`. 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 `score` (see [Environments] 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 `browser`. The most recent function, `score`, will be highlighted.
When you run `play`, `play` will call `get_symbols` and then `score`. As R works through `score`, it will come across the call to `browser` and run it. When R runs this call, several things will happen, as in Figure \@ref(fig:browser). First, R will stop running `score`. Second, the command prompt will change to `browser[1]>` 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 `score` in the scripts pane, and it will highlight the line that contains `browser()`. 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 `score` (see [Environments] 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 `browser`. The most recent function, `score`, will be highlighted.

I'm now in a new R mode, called _browser mode_. Browser mode is designed to help you uncover bugs, and the new display in RStudio is designed to help you navigate this mode.

Expand All @@ -155,7 +155,7 @@ Browse[1]> same
## [1] FALSE
```

Second, you can run code and see the same results that `score` would see. For example, you could run the remaining lines of the `score` 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 \@ref(fig:browser-buttons).
Second, you can run code and see the same results that `score` would see. For example, you could run the remaining lines of the `score` 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 \@ref(fig:browser-buttons).

The first button, Next, will run the next line of code in `score`. The highlighted line in the scripts pane will advance by one line to show you your new location in the `score` function. If the next line begins a code chunk, like a +for+ loop or an `if` tree, R will run the whole chunk and will highlight the whole chunk in the script window.

Expand All @@ -175,7 +175,7 @@ Once you fix the bug, you should resave your function a third time—this time w

## Break Points

RStudio's break points provide a graphical way to add a `browser` 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 \@ref(fig:break-point)).
RStudio's break points provide a graphical way to add a `browser` 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 \@ref(fig:break-point)).

R will treat the break point like a `browser` 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.

Expand Down Expand Up @@ -208,7 +208,7 @@ isdebugged(sample)

If this is all too much of a hassle, you can do what I do and use `debugonce` instead of `debug`. 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 `debugonce` on it a second time.

You can recreate `debugonce` in RStudio whenever an error occurs. "Rerun with debug" will appear in the grey error box beneath Show Traceback (\@ref(fig:show-traceback)). If you click this option, RStudio will rerun the command as if you had first run `debugonce` 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 `undebug` when you are done.
You can recreate `debugonce` in RStudio whenever an error occurs. "Rerun with debug" will appear in the grey error box beneath Show Traceback (Figure \@ref(fig:show-traceback)). If you click this option, RStudio will rerun the command as if you had first run `debugonce` 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 `undebug` when you are done.


## trace
Expand Down

0 comments on commit 0c6224c

Please sign in to comment.